@Entity
@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;
}
}
但对于hibernate 5,
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue(
strategy= GenerationType.AUTO,
generator="native"
)
@GenericGenerator(
name = "native",
strategy = "native"
)
private Long id;
private String title;
public Post() {}
public Post(String title) {
this.title = title;
}
}
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', 1)
INSERT INTO post (title, id)
VALUES ('High-Performance Java Persistence', 2)
INSERT INTO post (title, id)
VALUES ('High-Performance Java Persistence', 3)
所以使用
@GenericGenerator(
name = "native",
strategy = "native"
)
将生成以下查询:
INSERT INTO post (title)
VALUES ('High-Performance Java Persistence')
INSERT INTO post (title)
VALUES ('High-Performance Java Persistence')
INSERT INTO post (title)
VALUES ('High-Performance Java Persistence')
2条答案
按热度按时间xriantvc1#
在休眠5之前,
但对于hibernate 5,
如果不使用genericgenerator,则在执行多个保存请求时,hibernate将执行以下查询:
所以使用
将生成以下查询:
因此,执行更少的查询来执行相同的操作,从而获得高性能。。。
qxsslcnc2#
如果手动创建模式,则必须将auto increment属性设置为id列。如果您让hibernate生成您的模式,它将自己生成并设置它。