HIbernate Annotations @GeneratedValue策略类型之间的区别

时间:2016-12-04 07:01:55

标签: java mysql hibernate annotations javax.persistence

我是Hibernate Framework的新手。我正在使用Hibernate 5和MySQL 5.7.14-log。

我正在学习主键@Id @GeneratedValue。我尝试过生成类型策略,例如AUTO,SEQUENCE,IDENTITY,TABLE。

以下是我对AUTO,SEQUENCE,TABLE的观察 创建的结果如下

    @Id @GeneratedValue
Hibernate: drop table if exists hibernate_sequence
Hibernate: drop table if exists USER_DETAILS
Hibernate: create table hibernate_sequence (next_val bigint)
Hibernate: insert into hibernate_sequence values ( 1 )
Hibernate: create table USER_DETAILS (userId integer not null, address varchar(255), joiningDate date, userName varchar(255), primary key (userId))
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into USER_DETAILS (address, joiningDate, userName, userId) values (?, ?, ?, ?)
Hibernate: insert into USER_DETAILS (address, joiningDate, userName, userId) values (?, ?, ?, ?)
Hibernate: select userdetail0_.userId as userId1_0_0_, userdetail0_.address as address2_0_0_, userdetail0_.joiningDate as joiningD3_0_0_, userdetail0_.userName as userName4_0_0_ from USER_DETAILS userdetail0_ where userdetail0_.userId=?
UserDetails [userId=1, userName=First user, address=Adddress, joiningDate=2016-12-04]

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
Hibernate: drop table if exists hibernate_sequence
Hibernate: drop table if exists USER_DETAILS
Hibernate: create table hibernate_sequence (next_val bigint)
Hibernate: insert into hibernate_sequence values ( 1 )
Hibernate: create table USER_DETAILS (userId integer not null, address varchar(255), joiningDate date, userName varchar(255), primary key (userId))
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into USER_DETAILS (address, joiningDate, userName, userId) values (?, ?, ?, ?)
Hibernate: insert into USER_DETAILS (address, joiningDate, userName, userId) values (?, ?, ?, ?)
Hibernate: select userdetail0_.userId as userId1_0_0_, userdetail0_.address as address2_0_0_, userdetail0_.joiningDate as joiningD3_0_0_, userdetail0_.userName as userName4_0_0_ from USER_DETAILS userdetail0_ where userdetail0_.userId=?
UserDetails [userId=1, userName=First user, address=Adddress, joiningDate=2016-12-04]

    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
Hibernate: drop table if exists hibernate_sequence
Hibernate: drop table if exists USER_DETAILS
Hibernate: create table hibernate_sequence (next_val bigint)
Hibernate: insert into hibernate_sequence values ( 1 )
Hibernate: create table USER_DETAILS (userId integer not null, address varchar(255), joiningDate date, userName varchar(255), primary key (userId))
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into USER_DETAILS (address, joiningDate, userName, userId) values (?, ?, ?, ?)
Hibernate: insert into USER_DETAILS (address, joiningDate, userName, userId) values (?, ?, ?, ?)
Hibernate: select userdetail0_.userId as userId1_0_0_, userdetail0_.address as address2_0_0_, userdetail0_.joiningDate as joiningD3_0_0_, userdetail0_.userName as userName4_0_0_ from USER_DETAILS userdetail0_ where userdetail0_.userId=?
UserDetails [userId=1, userName=First user, address=Adddress, joiningDate=2016-12-04]

GenerationType.AUTO/SEQUENCE给出相同的结果(即创建一个新的表hibernate_sequence)。而TABLE创建了一个表hibernate_sequences

我有以下疑问。

  1. AUTO和SEQUENCE之间有什么区别吗?如果是,那么差异是什么以及它们具体针对哪些数据库?

  2. MySQL的默认策略是什么?

  3. 对于GenerationType.TABLE,使用2列sequence_name和next_val创建hibernate_sequences表。 sequence_name列指示什么。 [当前值为默认值]

  4. 这是我的POJO课程

    @Entity (name="USER_DETAILS")
    public class UserDetails
    {
        @Id @GeneratedValue
        private int     userId;
        private String  userName;   
        private String address;
    
        @Temporal (TemporalType.DATE)
        private Date joiningDate;
    
        public String getAddress()
        {
            return address;
        }
    
        public void setAddress(String address)
        {
            this.address = address;
        }
    
        public Date getJoiningDate()
        {
            return joiningDate;
        }
    
        public void setJoiningDate(Date joiningDate)
        {
            this.joiningDate = joiningDate;
        }
    
        public int getUserId()
        {
            return userId;
        }
    
        public void setUserId(int userId)
        {
            this.userId = userId;
        }
    
        public String getUserName()
        {
            return userName + " from getter";
        }
    
        public void setUserName(String userName)
        {
            this.userName = userName;
        }
    }
    

    我在网上搜索但我更加困惑。 看到的链接

    The differences between GeneratedValue strategies

    http://docs.oracle.com/javaee/5/api/javax/persistence/GenerationType.html

1 个答案:

答案 0 :(得分:1)

令人困惑,因为它是一种解决方法。解决方法处理在将行(也称为新的hibernate实体)插入数据库时​​出现的问题。

有什么问题?

问题是insert-statement不会返回新的id!

拥有此java代码:

Car car = new Car();
car.setName("Hot car");
System.out.println(car.getId()); // returns null
entityManager.persist(car));
System.out.println(car.getId()); // returns 367

我们期望的直观sql是:

INSERT INTO car (id, name) VALUES (null, "Hot car");

正如您所看到的,对于id( 367 )完全插入的行,没有像SELECT MAX(ID) FROM car;这样的声明。我们无法选择MAX(ID),因为可以通过其他交易同时更改最大ID!

相信我,没有数据库通用解决方案。

Q1

Auto试图找出最佳实践,它会查找hibernate_sequences-table,如果找不到它,它会查找内置序列。

Q2 MySQL

如果您使用的是InnoDB,则应使用TABLE,因为您可以为hibernate_sequences表创建一个foregin密钥。否则,它支持本机内置序列,这些序列应该是首选,并且表的pk绑定到此序列。

Q3

sequence_name是使用hibernate处理的每个表的主键(也是组合pk)的名称。如果您有三个表,则hibernate_sequences中有三行。例如,sequence_name的{​​{1}}为Car

seq_car给出了上述问题的解决方案,它保存了在next_val中具有pk序列的表中尚未使用的下一个原始密钥。如果数据库中有200个UserDetails,sequence_name可能是next_val