问题
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