How to find the entity with the greatest primary key?
Posted
by simpatico
on Stack Overflow
See other posts from Stack Overflow
or by simpatico
Published on 2010-05-20T19:16:51Z
Indexed on
2010/05/20
19:20 UTC
Read the original article
Hit count: 194
I've an entity LearningUnit that has an int primary key. Actually, it has nothing more.
Entity Concept has the following relationship with it: @ManyToOne @Size(min=1,max=7) private LearningUnit learningUnit;
In a constructor of Concept I need to retrieve the LearningUnit with the greatest primary key. If no LearningUnit exists yet I instantiate one. I then set this.learningUnit to the retrieved/instantied.
Finally, I call the empty constructor of Concept in a try-catch block, to have the entitymanager do the cardinality check. If an exception is thrown (I expect one in the case that already another 7 Concepts are referring to the same LearningUnit. In that case, I case instantiate a new LearningUnit with a new greater primary key.
Please, also point out, if any, clear pitfalls in my outlined algorithm above.
© Stack Overflow or respective owner