This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes EJB and Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB and Database" Watch "EJB and Database" New topic

EJB and Database

Mahesh Kulkarni
Ranch Hand

Joined: Jul 05, 2001
Posts: 62
We are developing a web based application in EJB. I've a query for u all.Please help me out.
If in a database(Oracle 8i) I'm creating a sequence for the primary key.Since it is a sequence written in Oracle.Can I access the sequence from my entity bean.
But if I want to make this field as the bean managed field(Entity Bean). Is it possible?.If yes How the create method in my home interface look like? Generally while accessing the sequence we use sequencename.nextValue() in a servlet. But how it is to be done in EJB since it is a Primary Key. Is it possible to have sequence as the Primary Key?
jon boydell

Joined: Jul 04, 2001
Posts: 15
It is possible to have a sequence as a primary key value. you need to get the value of the sequence BEFORE you insert the entity bean into the database. try using "select seq.nextval from dual" to get the next value of the sequence, put that into a variable in your ejb and then use that in the insert statement in your ejbCreate method.
Mahesh Kulkarni
Ranch Hand

Joined: Jul 05, 2001
Posts: 62
Thanx jon,
I'll try it. But before calling the seq.nextvalue from dual I've to make the connection with the database.That's OK.
One more query is that if I've Composite keys as Primary keys then How to access the same?
Karl Laird
Ranch Hand

Joined: Jul 14, 2001
Posts: 67
Typically a composite key would not be an autogenerated sequence, the two / more values are derived from somewhere else. In theory you should know these derived values - otherwise why is the entity being created. (IE if it is a bridging entity you should know the other entities it is bridging, and hence ask them for their portions of the composite key)

The Eagle sneers at the Peacock<p>Systems Administrator<br />OrderWare Solutions Ltd<br /><a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: EJB and Database
jQuery in Action, 3rd edition