Java – bypassing generatedvalue in Hibernate

It is possible to bypass the ID of @ generatedvalue in hibernate. We have a case where we want to use generatedvalue to set the ID most of the time, but in some cases, we need to set the ID manually

Is that possible?

Solution

I know you can do this in the JPA specification, so you should be able to do it in Hibernate (using JPA annotations)

If you have just filled in the ID field of the new persistent model you are creating, it will use the ID you set when you "merge" the model into the entitymanager

There are indeed differences You just used the ID, but the sequence specified by the generatedvalue annotation is unknown Unless you specify an unsigned ID lower than the current sequence value, you will encounter problems when the sequence reaches the value just used

So maybe I can see that you may want users to specify an ID, but you need to catch possible exceptions (duplicate IDS) that may occur in the future

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>