This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best Practices for JPA Generated Keys

 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been working with Apache OpenJPA and a lot of what I do involves tables with generated integer keyfields. One of the problems with them is that since they're only generated when the backing store object has been created, the actual identity of the record is only available by retrieving the generated key.

For brute-force batch, I've been doing this:


However, I'm trying to use Spring, and the Spring equivalent goes something like this:


The problem here is that Spring is wrapping the entire method as a transaction, so the record hasn't been committed until after the return and thus no key is retrievable from within the method.

I want the save() method to be totally self-contained, but I'd rather not have to make it work by writing manual transaction code if I can avoid it.

Does anyone know a nice clean solution?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic