Hibernate 无法使用data.sql生成UUID id
没有Hibernate 无法使用data.sql生成UUID id,hibernate,spring-boot,Hibernate,Spring Boot,没有data.sql一切正常 我正在使用SpringBoot2和H2数据库 但是当我试图使用data.sql初始化author表时,我得到了错误: Caused by: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "ID"; SQL statement: insert into author (`name`) values ('Pushkin') 以下是作者实体: @Entity public class Author
data.sql
一切正常
我正在使用SpringBoot2和H2数据库
但是当我试图使用data.sql
初始化author
表时,我得到了错误:
Caused by: org.h2.jdbc.JdbcSQLException:
NULL not allowed for column "ID"; SQL statement: insert into author (`name`) values ('Pushkin')
以下是作者
实体:
@Entity
public class Author {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
@Column(unique=true)
private String name;
//getters/setters/constructors
}
下面是data.sql
:
insert into author (`name`) values ('Pushkin');
UUIDGenerator仅在使用JPA/Hibernate存储实体时工作 在data.SQL中执行纯SQL时,不使用Hibernate。
您必须遵循JB Nizet的建议,UUIDGenerator仅在使用JPA/Hibernate存储实体时才起作用 在data.SQL中执行纯SQL时,不使用Hibernate。
您必须遵循JB Nizet的建议首先请检查您是否在数据库中为要生成的id勾选了自动递增,然后在@id中写入以下内容,
@GeneratedValue(策略=GenerationType.IDENTITY)。它将使您的id唯一。首先,请检查您是否在数据库中为要生成的id勾选了自动增量,然后在@id中写入以下内容, @GeneratedValue(策略=GenerationType.IDENTITY)。这将使你的身份证独一无二