Skip to main content

Why should not use the AUTO JPA GenerationType with MySQL and Hibernate

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/

Comments

Post a Comment

Popular posts from this blog

Digital Marketing

What actually is Digital Marketing? This post will help you understand the insights of Digital Marketing What is Digital Marketing? Digital Marketing is an integral part of the overall marketing strategies of any business. It basically covers the advertisement of products/services/business/brand via digital channels. The digital channels could be of any type like websites, search engines, social media, emails, SMS, and MMS. In case if you're using all these digital channels for the marketing, make sure to have all the statistics & workflow of your campaigns via marketing automation. What are the types of digital marketing? Well, there are 6 core digital marketing types: Search Engine Optimization (SEO) : Search Engine Optimization is nothing but a long-term process of improving your website rankings on search engine results pages (SERPs), which in turn has a wide range of tactics & strategies to implement. Although there is no specific method or a  spec

Changing the Time Zone on Amazon Linux Ec2 Instance

Amazon Linux instances are set to the UTC (Coordinated Universal Time) time zone by default, but you may wish to change the time on an instance to the local time or to another time zone in your network. Important These procedures are intended for use with Amazon Linux. For more information about other distributions, see their specific documentation. To change the time zone on an instance Identify the time zone to use on the instance. The  /usr/share/zoneinfo  directory contains a hierarchy of time zone data files. Browse the directory structure at that location to find a file for your time zone. [ec2-user ~]$ ls /usr/share/zoneinfo Africa Chile GB Indian Mideast posixrules US America CST6CDT GB-Eire Iran MST PRC UTC Antarctica Cuba GMT iso3166.tab MST7MDT PST8PDT WET Arctic EET GMT0 Israel Navajo right W-SU ... Some of the entries at this location are directo

How do I change the time zone of my Amazon RDS database instance?

As we know bydefault time in the format of UTC in mysql.We can set local time zone to our AWS RDS Instance for our application. or any other time zone prefared Cloud Based Website Hosting Service Provider Steps 1: Go to Services and Select RDS Now to change time zone we have to change "Parameter Group" in left side that is associated with DB instance first we can check default Parameter Group for our instance is Parameter group default.mysql5.7  ( in-sync ) like this. So we have to change the time zone in this Parameter Group.  now open that parameter group (default.mysql5.7)  and click on edit parameter. then search for time_zone (because we want to change it.) then we have to change time_zone only by default it is engine-default (that is utc)  we have to select Asia/Calcutta.  More information we can ref.  https://aws.amazon.com/premiumsupport/knowledge-center/rds-change-time-zone/