File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes @GeneratedValue with max value from table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "@GeneratedValue with max value from table" Watch "@GeneratedValue with max value from table" New topic

@GeneratedValue with max value from table

Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 183
Is there a way to configure the @Id field to have max value of table + 1.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Why do you want it to do this? Is it not enough to just be assured of a unique value?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Hauke Ingmar Schmidt

Joined: Nov 18, 2008
Posts: 436
As Paul said, it is normally not needed.

If you want to do so you need to set a different strategy for the generated value. If you use a TABLE strategy (and configure it correctly) an extra table will be created that takes the sequence name and the last assigned value and will set a new id to the next value. Or you can set an id in the PrePersist JPA event. (Or even in a db trigger.)

But unless there are extremely important reasons for the constraint and the decision is made by people who fully understand the implications this manual approach to id generation has, this is a bad idea.
I agree. Here's the link:
subject: @GeneratedValue with max value from table
jQuery in Action, 3rd edition