SQL0407:Null JPA spring 数据插入操作的列或变量中不允许使用值

SQL0407:Null values not allowed in column or variable for JPA spring data insert operation

我正在使用 spring 数据进行交易,我正在尝试执行保存操作(插入操作)。但是我得到 [SQL0407] 列或变量 TLLMTS 中​​不允许空值。

实体如下

@Entity
@IdClass(OsytxlId.class)
@Table(name="OSYTXL")
@NamedQuery(name="Osytxl.findAll", query="SELECT o FROM Osytxl o")
public class Osytxl implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="TLCONO")
    private BigDecimal tlcono;

    @Id
    @Column(name="TLDIVI")
    private String tldivi;

    @Id
    @Column(name="TLLINO")
    private BigDecimal tllino;

    @Column(name="TLLMTS")
    private BigDecimal tllmts;

    @Id
    @Column(name="TLLNCD")
    private String tllncd;

    @Column(name="TLTX60")
    private String tltx60;

    @Id
    @Column(name="TLTXID")
    private BigDecimal tltxid;

    @Id
    @Column(name="TLTXVR")
    private String tltxvr;

    //getter and setters

}

我正在从服务实现中调用以下代码部分 class

            Osytxl osytxl = null;

            for (int lineNo = 0; lineNo < lines.length; lineNo++) {
                osytxl = new Osytxl();
                osytxl.setTlcono(osytxh.getThcono());
                osytxl.setTldivi(osytxh.getThdivi());
                osytxl.setTltxid(osytxh.getThtxid());
                osytxl.setTltxvr(osytxh.getThtxvr());
                osytxl.setTllncd(osytxh.getThlncd());
                osytxl.setTllmts(new BigDecimal("1437651510403"));
                osytxl.setTllino(new BigDecimal(lineNo+1));
                osytxl.setTltx60(lines[lineNo]);
                osytxlList.add(osytxl);
            }
            if(osytxlList.size()>0)
                osytxlRepository.save(osytxlList);

我正在使用 JPA 存储库但是我收到以下异常

Hibernate: insert into OSYTXL (TLLMTS, TLTX60, TLCONO, TLDIVI, TLLINO, TLLNCD, TLTXID, TLTXVR) values (?, ?, ?, ?, ?, ?, ?, ?)
[2015-07-24 22:54:59][http-nio-8080-exec-10][WARN] SQL Error: -407, SQLState: 23502
[2015-07-24 22:54:59][http-nio-8080-exec-10][ERROR] [SQL0407] Null values not allowed in column or variable TLLMTS.
[2015-07-24 22:54:59][http-nio-8080-exec-10][INFO] HHH000010: On release of batch it still contained JDBC statements
[2015-07-24 22:55:01][http-nio-8080-exec-10][DEBUG] Initiating transaction rollback after commit exception
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: could not execute statement; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:415) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.orm.jpa.DefaultJpaDialect.translateExceptionIfPossible(DefaultJpaDialect.java:122) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) [spring-tx-4.1.6.RELEASE.jar:?]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) [spring-tx-4.1.6.RELEASE.jar:?]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:521) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.6.RELEASE.jar:?]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [spring-aop-4.1.6.RELEASE.jar:?]
    at c

........................................................



Caused by: java.sql.SQLIntegrityConstraintViolationException: [SQL0407] Null values not allowed in column or variable TLLMTS.
    at com.ibm.as400.access.JDError.createSQLExceptionSubClass(JDError.java:855) ~[jt400-8.3.jar:JTOpen 8.3]
    at com.ibm.as400.access.JDError.throwSQLException(JDError.java:706) ~[jt400-8.3.jar:JTOpen 8.3]
    at com.ibm.as400.access.JDError.throwSQLException(JDError.java:676) ~[jt400-8.3.jar:JTOpen 8.3]

调用保存操作后,它成功执行了 select 操作,但似乎没有持久化对 Entity bean 对象的更改。

数据库是这样设置的

CREATE TABLE "M3FDBDEM"."OSYTXL"(
TLCONO DECIMAL default '0' not null,
TLDIVI NCHAR default '''' not null,
TLTXID DECIMAL default '0' not null,
TLTXVR NCHAR default '''' not null,
TLLNCD NCHAR default '''' not null,
TLLINO DECIMAL default '0' not null,
TLTX60 NCHAR default '''' not null,
TLLMTS DECIMAL default '0' not null)

我认为我的做法是正确的。谁能帮忙.

您可以启用休眠跟踪以查看使用以下配置发送到数据库的实际值。

log4j.logger.org.hibernate.type=TRACE

代码看起来非常简单,请确保您在 setTllmts 方法中设置了正确的变量 tllmts

建议: 为什么使用 BigDecimal 作为类型?为什么不 float

以下是堆栈跟踪中的明确消息

  1. org.hibernate.exception.ConstraintViolationException:
  2. 列或变量 TLLMTS 中​​不允许空值。

您可能试图插入 table 将 TLLMTS 的空值,而您的数据库列为 nullable false,这就是您收到此错误的原因。

尝试记录您要保存的对象

我找到了原因,当我为它起作用的实体对象设置默认值时。根据结构列的默认值,一旦它的集合看起来有效

    @Id
    @Column(name="TLCONO")
    private BigDecimal tlcono = new BigDecimal(0);

    @Id
    @Column(name="TLDIVI")
    private String tldivi = "";

    @Id
    @Column(name="TLLINO")
    private BigDecimal tllino=new BigDecimal(0);

    @Column(name="TLLMTS")
    private BigDecimal tllmts = new BigDecimal(System.currentTimeMillis() / 1000L);;

    @Id
    @Column(name="TLLNCD")
    private String tllncd = "";

    @Column(name="TLTX60")
    private String tltx60 = "";

    @Id
    @Column(name="TLTXID")
    private BigDecimal tltxid = new BigDecimal(0);

    @Id
    @Column(name="TLTXVR")
    private String tltxvr = "";