H2 database: NULL not allowed for column “ID” when inserting record using jdbcTemplate

前端 未结 2 1745
一个人的身影
一个人的身影 2020-12-10 01:18

I use hibernate\'s hbm2ddl to generate schema automatically. Here is my domain:

@Entity
public class Reader {

  @Id
  @GeneratedValue(strategy=GenerationTyp         


        
相关标签:
2条回答
  • 2020-12-10 01:50

    Hibernate 5.2.x (Spring Boot 2.x) change default strategy for sequences, if DB supported one. So, with strategy=GenerationType.AUTO, hibernate_sequence is created, but id is not autoincremented, based on this sequence, as must be:

    create table users (id integer not null, ...) 
    

    instead of

    create table table_name(id int default hibernate_sequence.nextval primary key, ...);
    

    (see HHH-13268). There are several solutions:

    • change @GeneratedValue to strategy = GenerationType.IDENTITY
    • set spring.jpa.properties.hibernate.id.new_generator_mappings=false (spring-boot alias spring.jpa.hibernate.use-new-id-generator-mappings)
    • insert with nextval: INSERT INTO TABLE(ID, ...) VALUES (hibernate_sequence.nextval, ...)
    0 讨论(0)
  • 2020-12-10 02:00

    Try to use strategy=GenerationType.IDENTITY instead of the strategy=GenerationType.AUTO

    Also could be wrong hibernate.dialect Try the

    hibernate.dialect=org.hibernate.dialect.H2Dialect
    
    0 讨论(0)
提交回复
热议问题