gpt4 book ai didi

java - HIbernate注解@GeneratedValue策略类型的区别

转载 作者:行者123 更新时间:2023-11-29 02:46:51 55 4
gpt4 key购买 nike

我是 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,创建的 hibernate_sequences 表包含 2 列,sequence_name 和 next_val。 sequence_name 列表示什么。 [当前值为默认值]

这是我的 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

最佳答案

它令人困惑,因为它是一种变通方法。变通方法处理在向数据库中插入行(又名新的 hibernate 实体)时出现的问题。

问题是什么?

问题是插入语句没有返回新的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");

如您所见,没有像 SELECT MAX(ID) FROM car; 这样的语句用于 ID (367) 现在插入的行。我们不能选择 MAX(ID) 因为最大 id 可能同时被另一个事务更改!

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

Q1

Auto 会尝试找出最佳实践,它会查找 hibernate_sequences 表,如果找不到,则会查找内置序列。

Q2 MySQL

如果您正在使用 InnoDB,您应该使用 TABLE,因为您可以为 hibernate_sequences 表创建一个外键。否则,它支持 native 内置序列,应该首选将表的 pk 绑定(bind)到此序列。

Q3

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

next_val 给出了上述问题的解决方案,它保存了在 sequence_name 中具有 pk 序列的表中尚未使用的下一个主键.如果数据库中有 200 个 UserDetails,则 next_val 可能是 201

关于java - HIbernate注解@GeneratedValue策略类型的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956272/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com