How to create portable and custom identity generation in JPA 2 / Hibernate?

I would like to have native and portable identifier creation on my JPA 2 entities currently running Hibernate and MySQL

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

If I solve it with @GeneratedValue (strategy = IDENTITY), I lose Oracle / Postgres portability

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

+4
source share
2 answers

You can write your own generator and, possibly, call a function / stored procedure on your DB to create the identifier you need.
Take a look here , this is a blog with a good example on how to do what I just wrote.

0
source

Without changing the code in sleep mode, you cannot affect this. One way is to use different sets of mappings (xml instead of annotations) for each database with different identifier requirements. This, of course, is quite a lot.

The only truly portable way that works regardless of the database provider is to create an identifier using the TABLE strategy.

0
source

All Articles