I use Eclipselink as my JPA provider, how can I make a field auto increment?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, I get that, this is what you wrote in your question. But it can be interpreted in several ways and repeating the same thing without clarifying doesn’t help much 🙂
If you want to increment a field per entity record (e.g. a “counter” for
Orderwith id=1, another one forOrderwith id=2), what I suggested would work.If you want a behavior similar to a primary key (i.e. an auto-incremented column), it won’t. And in that case, standard JPA doesn’t offer any particular facility (standard JPA only allows
GenereatedValueonIdannotated field). The only way I can think of would be to insert another dedicated entity just to get the primary key from it.Your exact requirement is unclear but I’m assuming you’re talking about a random field, not the primary key. In that case, you could maybe use life cycle callbacks methods: