相关疑难解决方法(0)

H2数据库:使用jdbcTemplate插入记录时,列"ID"不允许NULL

我使用hibernate的hbm2ddl自动生成模式.这是我的域名:

@Entity
public class Reader {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  Long id;

  @Column(nullable=false,unique=true)
  String name;

  @Enumerated(EnumType.STRING)
  Gender gender;

  int age;

  Date registeredDate = new Date();

// getter and setter ...
}
Run Code Online (Sandbox Code Playgroud)

当我使用hibernate来保存a时reader,它可以正常工作,因为它会产生一个id reader.但是,当我使用jdbcTemplate插入带有纯SQL的记录时,它会报告错误:

org.springframework.dao.DataIntegrityViolationException: StatementCallback; 
SQL [insert into reader(name,gender,age) values('Lily','FEMALE',21)]; 
NULL not allowed for column "ID"; 
    SQL statement:insert into reader(name,gender,age) values('Lily','FEMALE',21) [23502-192]; 
nested exception is org.h2.jdbc.JdbcSQLException: NULL not allowed for column "ID"; 
    SQL statement:  insert into reader(name,gender,age) values('Lily','FEMALE',21) [23502-192]
Run Code Online (Sandbox Code Playgroud)

怎么解决这个?

  1. 我调试发现生成的hb2ddl的DDL是create table Book (id bigint not null, author …

hibernate h2

17
推荐指数
4
解决办法
2万
查看次数

标签 统计

h2 ×1

hibernate ×1