Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Auto-increment on CMP EJB + Primary key

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's Jboss version i can do it ? ( Krzysiek Hycnar )
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic