How to make portable AND native ID generation in JPA 2 / Hibernate?

纵然是瞬间 提交于 2020-02-03 07:52:24

问题


I would like to have native and portable id generation on my JPA 2 entities, currently running Hibernate and MySQL

When using @GeneratedValue(strategy=AUTO), hibernate defaults to the "hibernate_sequence" table on MySQL, i would like IDENTITY

If i solve it using @GeneratedValue(strategy=IDENTITY), i loose Oracle/Postgres portability

How can i set Hibernate to use IDENTITY as default for mysql when @GeneratedValue strategy=AUTO?


回答1:


You can write your own custom generator and maybe invoke a function/stored procedure on your DB to create the identity you need.
Take a look here , this is a blog with nice example about how to do what I just wrote.




回答2:


Without code change in Hibernate you cannot affect that. One way around is using different set of mappings (xml instead of annotations) for each database with different id requirements. That is of course quite much to do.

Only truly portable way that works independently from database vendor, is to generate id with TABLE strategy.



来源:https://stackoverflow.com/questions/11216375/how-to-make-portable-and-native-id-generation-in-jpa-2-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!