Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate H2数据库:列“不允许为空”;ID";使用jdbcTemplate插入记录时_Hibernate_H2 - Fatal编程技术网

Hibernate H2数据库:列“不允许为空”;ID";使用jdbcTemplate插入记录时

Hibernate H2数据库:列“不允许为空”;ID";使用jdbcTemplate插入记录时,hibernate,h2,Hibernate,H2,我使用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; D

我使用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 ...
}
当我使用hibernate保存
读卡器
时,它工作正常,因为它为
读卡器
生成了一个id。但是,当我使用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]
如何解决这个问题

  • 我调试发现生成的hb2ddl的DDL是
    create table Book(id bigint not null,author varchar(255),name varchar(255),price double not null,type varchar(255),primary key(id))
    。似乎Hibrate以自己的方式处理id策略,但如何处理
  • @GeneratedValue(strategy=GenerationType.AUTO)
    应该在DDL语句中生成
    自动增量
    ,但我没有找到它。我错过了吗

  • 尝试使用
    strategy=GenerationType.IDENTITY
    而不是
    strategy=GenerationType.AUTO

    也可能是错误的hibernate 试试

    Hibernate 5.2.x(Spring Boot 2.x)更改序列的默认策略(如果DB支持)。因此,使用
    strategy=GenerationType.AUTO
    ,将创建
    hibernate\u序列
    ,但
    id
    不会根据此序列自动递增,因为必须是:

    create table users (id integer not null, ...) 
    
    而不是

    create table table_name(id int default hibernate_sequence.nextval primary key, ...);
    
    (见附件)。有几种解决方案:

    • @GeneratedValue
      更改为
      strategy=GenerationType.IDENTITY
    • 设置
      spring.jpa.properties.hibernate.id.new\u generator\u mappings=false
      (spring引导别名
      spring.jpa.hibernate.use new id generator mappings
    • 使用nextval插入:
      插入表(ID,…)值(hibernate\u sequence.nextval,…)

    谢谢。当我更改为
    策略=GenerationType.IDENTITY
    时,它会起作用。DDL子句现在改为
    create table Reader(默认情况下生成的id bigint为identity,age integer不为null,gender varchar(255),name varchar(255)不为null,registeredDate timestamp,primary key(id))
    。那么这里的
    IDENTITY
    AUTO
    有什么区别呢?IDENTITY表示持久化提供者必须使用数据库标识列为实体分配主键。AUTO表示持久性提供程序应为特定数据库选择适当的策略。
    AUTO
    生成策略可能期望数据库资源存在,也可能试图创建一个数据库资源。如果供应商不支持模式生成或无法在运行时创建模式资源,则供应商可能会提供有关如何创建此类资源的文档。
    create table table_name(id int default hibernate_sequence.nextval primary key, ...);