Introduction
As I already mentioned, you should never use the
TABLE
identifier generator since it does not scale properly. In this post, I’ll show you why you should not rely on the AUTO
GenerationType
strategy if you’re Hibernate application uses MySQL.Prior to Hibernate 5
On Hibernate 4, if you had the following entity mapping:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| @Entity (name = "Post" ) @Table (name = "post" ) public class Post { @Id @GeneratedValue (strategy = GenerationType.AUTO) private Long id; private String title; public Post() {} public Post(String title) { this .title = title; } } |
When persisting 3
Post
entities:
1
2
3
4
5
6
7
8
9
| for ( int i = 1 ; i <= 3 ; i++ ) { entityManager.persist( new Post( String.format( "High-Performance Java Persistence, Part %d" , i ) ) ); } |
Hibernate would generate the following insert statements:
1
2
3
4
5
6
7
8
| INSERT INTO post (title) VALUES ( 'High-Performance Java Persistence, Part 1' ) INSERT INTO post (title) VALUES ( 'High-Performance Java Persistence, Part 2' ) INSERT INTO post (title) VALUES ( 'High-Performance Java Persistence, Part 3' ) |
That’s great! Hibernate used the IDENTITY column to generate the entity identifier which is the only reasonable option for MySQL.
Hibernate 5
If you run the same unit test on Hibernate 5, you’ll get the following SQL statements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| SELECT next_val as id_val FROM hibernate_sequence FOR UPDATE UPDATE hibernate_sequence SET next_val= 2 where next_val=1 SELECT next_val as id_val FROM hibernate_sequence FOR UPDATE UPDATE hibernate_sequence SET next_val= 3 where next_val=1 SELECT next_val as id_val FROM hibernate_sequence FOR UPDATE UPDATE hibernate_sequence SET next_val= 4 where next_val=3 INSERT INTO post (title, id) VALUES ( 'High-Performance Java Persistence, Part 1' , 1) INSERT INTO post (title, id) VALUES ( 'High-Performance Java Persistence, Part 2' , 2) INSERT INTO post (title, id) VALUES ( 'High-Performance Java Persistence, Part 3' , 3) |
What’s just happened? Well, Hibernate picks the
TABLE
generator instead of IDENTITY
when the underlying database does not support sequences. However, TABLE
generator is not a good choice. Check out the HHH-11014 Jira issue for more details related to this behavior change.How to fix it?
The fix is extremely easy. You just need to use the
native
identifier instead:
1
2
3
4
5
6
7
8
9
10
| @Id @GeneratedValue ( strategy= GenerationType.AUTO, generator= "native" ) @GenericGenerator ( name = "native" , strategy = "native" ) private Long id; |
Now, when running the previous test case, Hibernate uses the IDENTITY column instead:
1
2
3
4
5
6
7
8
| INSERT INTO post (title) VALUES ( 'High-Performance Java Persistence, Part 1' ) INSERT INTO post (title) VALUES ( 'High-Performance Java Persistence, Part 2' ) INSERT INTO post (title) VALUES ( 'High-Performance Java Persistence, Part 3' ) |
If you want to use a portable solution that manages to customize the
SEQUENCE
generator while still allowing you to pick the IDENTITY
generator for MySQL, then check out this article.
Source Link (Copied) https://vladmihalcea.com/why-should-not-use-the-auto-jpa-generationtype-with-mysql-and-hibernate/
Great Article
ReplyDeleteIEEE Android Projects for CSE
Java Training in Chennai
FInal Year Project Centers in Chennai
Java Training in Chennai