aspose file tools
The moose likes Object Relational Mapping and the fly likes JPA and Generated Key Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "JPA and Generated Key" Watch "JPA and Generated Key" New topic
Author

JPA and Generated Key

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 745
Hi guys! Is there a functionality in JPA that allows a user to get the generated key like what you could do in plain old JDBC? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://game-rumble.com/ - http://everypesocounts.com/
Christophe Verré
Marshal

Joined: Nov 24, 2005
Posts: 14361

The @GeneratedValue annotation which supports four different strategies to generate ids. (e.g. using a sequence)


[SCBCD Wall of Fame] [My Blog]
All roads lead to JavaRanch
Help Japan. Make a donation.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 745
Thanks! It seems that the generated key cannot be used without commiting a transaction first? I'm trying to insert a record then get the generated key and then update the record. But the record it seems is not being saved. I could make two different transactions and commit each but that would only rollback 1 transaction.


[ May 23, 2007: Message edited by: Timothy Sam ]
Christophe Verré
Marshal

Joined: Nov 24, 2005
Posts: 14361

Yes, the transaction must be committed before you can access the generated id.
 
 
subject: JPA and Generated Key
 
Threads others viewed
Pro EJB3 Book - chapters 3,4-8!
Entity Problem
custom join table
Why Timestamp isn't allowed as a primary key?
Generated ID in JPA + Hibernate - updated ID in the original object?
MyEclipse, The Clear Choice