我是 Hibernate 框架的新手。我正在使用带有 MySQL 5.7.14-log 的 Hibernate 5。
我正在学习主键@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
我有以下疑问。
AUTO 和 SEQUENCE 之间有什么区别吗?如果是,那么有什么区别以及它们特定于哪些数据库?
MySQL的默认策略是什么?
对于 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;
}
}
我在网上搜索过,但我更加困惑。看到的链接
http://docs.oracle.com/javaee/5/api/javax/persistence/GenerationType.html