H2数据库将null插入主键

时间:2018-04-18 12:27:26

标签: java spring-boot sequence h2

我在H2数据库中创建了表格,如下所示:

CREATE OR REPLACE SEQUENCE IF NOT EXISTS SCMSA_HIST.KEY_GEN_SEQ
    START WITH 0
   INCREMENT BY 1
    NOCYCLE
    NOCACHE;

CREATE TABLE IF NOT EXISTS SCMSA_HIST.SCMSA_POS_TRANS_ROLLUP
(
 POS_TRANS_ID INTEGER DEFAULT 
    (NEXT VALUE FOR SCMSA_HIST.KEY_GEN_SEQ) 
    NOT NULL IDENTITY ,
 JOB_LOG_ID INTEGER,
 DEALER_CODE VARCHAR(255),
 STORE_ID VARCHAR(255),
 TRANSACTION_DT TIMESTAMP,
 QUANTITY INTEGER,
 ROLLUP_TYPE VARCHAR(255),
 CREATE_DT TIMESTAMP,
 MAX_TRANSACTION_DT TIMESTAMP,
 PROCESSED_FLAG VARCHAR(255),
 CREATE_MONTH INTEGER,
 CREATE_YEAR INTEGER
);

上表的Model类如下:

@Entity
@Table(schema = "SCMSA_HIST", name = "SCMSA_POS_TRANS_ROLLUP")
public class ScmsaPosTransRollup {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ")
    @SequenceGenerator(name = "SEQ", sequenceName = "SCMSA_HIST.KEY_GEN_SEQ")
    @Column(name = "POS_TRANS_ID")
    private Long posTransId;

    @Column(name = "JOB_LOG_ID")
    private Long jobLogId;

    @Column(name = "DEALER_CODE")
    private String dealerCode;

    @Column(name = "STORE_ID")
    private String storeId;

    @Column(name = "TRANSACTION_DT")
    private Timestamp transactionDate;

    @Column(name = "ROLLUP_TYPE")
    private String rollupType;

    @Column(name = "QUANTITY")
    private Integer quantity;

    @Column(name = "CREATE_DT")
    private Timestamp createDate;

    @Column(name = "MAX_TRANSACTION_DT")
    private Timestamp maxTransactionDate;

    @Column(name = "PROCESSED_FLAG")
    private String processedFlag;

    @Column(name = "CREATE_MONTH", insertable = false, updatable = false)
    private Integer createMonth;

    @Column(name = "CREATE_YEAR", insertable = false, updatable = false)
    private Integer createYear;


    public ScmsaPosTransRollup() {
    }
//getter and setter
}

但是,当我尝试插入时," POS_TRANS_ID"插入为null。任何人都可以建议我在这里做错了什么。

1 个答案:

答案 0 :(得分:1)

我试图重现你的问题。对我有什么帮助: 1.脚本

CREATE OR REPLACE SEQUENCE IF NOT EXISTS KEY_GEN_SEQ
   MINVALUE 1 
   MAXVALUE 999999999999999999
   START WITH 1
   INCREMENT BY 500
NOCYCLE
NOCACHE;

在create table DDL中,我替换了ID创建

 POS_TRANS_ID INTEGER DEFAULT  KEY_GEN_SEQ.NEXTVAL 
    NOT NULL IDENTITY ,

在Java类中,将initialValue和allocationSize添加到@SequenceGenerator,并将策略更改为SEQUENCE

@GeneratedValue(strategy=SEQUENCE, generator="SEQ")
@SequenceGenerator(name = "SEQ", sequenceName = "KEY_GEN_SEQ",initialValue = 1,allocationSize = 500)

不要忘记在Hibernate props中定义方言

hibernate.dialect=org.hibernate.dialect.H2Dialect