Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 232
Julio Faerman Avatar asked Nov 03 '22 21:11

Julio Faerman


1 Answers

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.

like image 78
Yair Zaslavsky Avatar answered Nov 09 '22 10:11

Yair Zaslavsky