Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

@GeneratedValue with max value from table

 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to configure the @Id field to have max value of table + 1.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want it to do this? Is it not enough to just be assured of a unique value?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!