File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Auto-increment  on CMP EJB + Primary key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Auto-increment  on CMP EJB + Primary key" Watch "Auto-increment  on CMP EJB + Primary key" New topic
Author

Auto-increment on CMP EJB + Primary key

madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148
Hi all,

I working with CMP EJB and oracle.
I have a table that has an trigger to autoincrement the primary key.
In the create method of the EJB i am able to insert into the database.

Since i specify a dummy value for the primary key in the Create method for EJB the database actually assigns the next auto increment value. which for all purposes is fine with me.

Question ? .... How do i get the latest value for the primary key for the tuple that has been just inserted in the database. ?? Remember i put in a Dummy value but the real values is one inserted by database

is there any way i can get the values that i just stored in the DB in the create method.... in the same create call ???

EJB gurus please.. help me out.


Money for nothing and Java for Free
SCJP, SCWCD
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
No, there isn't.
For some reason wiser men than me don't understand the sages at Sun decided that autoincrement values don't exist in real databases and therefore didn't allow for them in the EJB specification.

What I (and many others) do is create generators in my database which I call before inserting a new record, setting the primary key myself instead of using autoincrement fields.
Of course that immediately puts a database dependency into your code as such things are not part of standard SQL and therefore need custom logic for each RDBMS that supports them (not all do).

For Firebird I do something like this:
Create a generator and set it to initial value of 0.


In the EJB:


In the dbHelper (which is created for different generators as requested):


That at least concentrates all the engine specific code in a single place where it can be easily swapped out by instantiating another class from the factory.
This last bit of code of course (and the DDL) are extremely DMBS dependent and will only work with Interbase and Firebird.


42
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
ejb containers may provide proprietary support for auto generation of primary keys, through oracle sequence, table managed sequences, etc.

If you are using weblogic/ejbgen, use

@ejbgen:automatic-key-generation type="Oracle" name="MY_SEQUENCE" cache-size="1"
Krzysiek Hycnar
Ranch Hand

Joined: Jan 02, 2004
Posts: 74
That's right you can rely only on vendor specific support.

Below you've got an example how to do this with JBoss and PostgreSQL (I've got a link with explanation how to achieve the same with JBoss and Oracle, but it's on another comp, if you need it I can look for it).

What you must do to compile it is to add a com.hicnar.library.Author class which is a simple transfer-object bean (you can infer from the last method how it's built).

Good Luck
Krzysiek

Tiago Sudeval
Greenhorn

Joined: Nov 09, 2006
Posts: 5
What's Jboss version i can do it ? ( Krzysiek Hycnar )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Auto-increment on CMP EJB + Primary key