org.hibernate.PropertyValueException:not-null属性引用null或transient值:仅@Scheduled的异常

时间:2014-08-28 07:00:27

标签: spring hibernate jpa

我正在编写一个JPA项目(用spring弹出hibernate)。

我面临的问题是当我使用interestCalculation()注释执行下面的@Scheduled方法时,我会遇到异常:

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.mad.app.domain.DemandLedgerMap.demandLedger; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.mad.app.domain.DemandLedgerMap.demandLedger

完全执行for中的quartsTestdDisplay()循环后。但是,当执行for循环后,执行相同的方法而没有调度数据时,会持久/更新。 只有从@Scheduled触发的方法才会抛出异常。

下面是我的代码,一个是deligate层类,另一个是服务层类。 在委托层中,我使用@Scheduled作为计划触发器interestCalculation()

  @Controller
  public class DcbDailyBalanceDelegate {

@Autowired
private DcbDailyBalanceService dcbDailyBalanceService;
/**
 * El name.
 */
public static final String EL_NAME = "#{dcbDailyBalanceDelegate}";

@Transactional
@Scheduled(cron="0 37 11 ? * THU")
public void interestCalculation()
{
    //dcbDailyBalanceService.dcbBalanceToDcbDailybalance();
    dcbDailyBalanceService.quartsTestdDisplay();
}

}

@Transactional
public void quartsTestToUpdate()
{

    for (int i = 0; i < 3000; i++) {

        DcbDailyBalance dcbDailyBalance=new DcbDailyBalance();

        DemandLedgerMap  demandLedgerMap=new DemandLedgerMap();

        DledgerApplicableMap dledgerApplicableMap=new DledgerApplicableMap();

        demandLedgerMap.setDldgrMapId(1);
        dledgerApplicableMap.setDlamapId(1);

        dcbDailyBalance.setDcbbalAmount(1000+i);
        dcbDailyBalance.setDcbbalDataSource((byte) 0);
        dcbDailyBalance.setDcbbalDate(1409120385);
        dcbDailyBalance.setDcbbalIntrstAmount(100+i);
        dcbDailyBalance.setDcbbalLastupdated(1409120385);
        dcbDailyBalance.setDemandLedgerMap(demandLedgerMap);
        dcbDailyBalance.setDledgerApplicableMap(dledgerApplicableMap);

        DcbDailyBalance dailyBalance=dailyBalanceDao.update(dcbDailyBalance);

     }
    }

这是例外:

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.mad.app.domain.DemandLedgerMap.demandLedger; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.mad.app.domain.DemandLedgerMap.demandLedger
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:206) ~[spring-orm-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:154) ~[spring-orm-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:519) ~[spring-orm-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646) ~[spring-aop-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at com.mad.app.controller.dcb.DcbDailyBalanceDelegate$$EnhancerByCGLIB$$7abb9f11.interestCalculation(<generated>) ~[spring-core-4.0.0.RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_55]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_55]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_55]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_55]
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) ~[spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) [spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.7.0_55]
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.7.0_55]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source) [na:1.7.0_55]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) [na:1.7.0_55]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.7.0_55]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.7.0_55]
    at java.lang.Thread.run(Unknown Source) [na:1.7.0_55]


Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value : com.mad.app.domain.DemandLedgerMap.demandLedger
    at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:106) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:309) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:160) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77) ~[hibernate-entitymanager-4.3.1.Final.jar:4.3.1.Final]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:515) ~[spring-orm-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    ... 22 common frames omitted

下面是DcbDailyBalance类

    import java.io.Serializable;

    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.NamedQuery;
    import javax.persistence.Table;

    import com.mad.app.common.domain.BusinessEntity;


/**
 * The persistent class for the dcb_daily_balances database table.
 * 
 */
@Entity
@Table(name="dcb_daily_balances")
@NamedQuery(name="DcbDailyBalance.findAll", query="SELECT d FROM DcbDailyBalance d")
public class DcbDailyBalance extends BusinessEntity  implements Serializable {
    private static final long serialVersionUID = 1L;
    private int dcbbalId;
    private double dcbbalAmount;
    private byte dcbbalDataSource;
    private int dcbbalDate;
    private int dcbbalLastupdated;
    private byte dcbbalRowStatus;
    private double dcbbalIntrstAmount;
    private DemandLedgerMap demandLedgerMap;
    private DledgerApplicableMap dledgerApplicableMap;


    public DcbDailyBalance() {
    }


    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="DCBBAL_ID")
    public int getDcbbalId() {
        return this.dcbbalId;
    }

    public void setDcbbalId(int dcbbalId) {
        this.dcbbalId = dcbbalId;
    }

    @Column(name="DCBBAL_AMOUNT")
    public double getDcbbalAmount() {
        return this.dcbbalAmount;
    }

    public void setDcbbalAmount(double dcbbalAmount) {
        this.dcbbalAmount = dcbbalAmount;
    }


    @Column(name="DCBBAL_DATA_SOURCE")
    public byte getDcbbalDataSource() {
        return this.dcbbalDataSource;
    }

    public void setDcbbalDataSource(byte dcbbalDataSource) {
        this.dcbbalDataSource = dcbbalDataSource;
    }


    @Column(name="DCBBAL_DATE")
    public int getDcbbalDate() {
        return this.dcbbalDate;
    }

    public void setDcbbalDate(int dcbbalDate) {
        this.dcbbalDate = dcbbalDate;
    }


    @Column(name="DCBBAL_LASTUPDATED")
    public int getDcbbalLastupdated() {
        return this.dcbbalLastupdated;
    }

    public void setDcbbalLastupdated(int dcbbalLastupdated) {
        this.dcbbalLastupdated = dcbbalLastupdated;
    }


    @Column(name="DCBBAL_ROW_STATUS")
    public byte getDcbbalRowStatus() {
        return this.dcbbalRowStatus;
    }

    public void setDcbbalRowStatus(byte dcbbalRowStatus) {
        this.dcbbalRowStatus = dcbbalRowStatus;
    }

    @Column(name="DCBBAL_INTRST_AMOUNT")
    public double getDcbbalIntrstAmount() {
        return this.dcbbalIntrstAmount;
    }

    public void setDcbbalIntrstAmount(double dcbbalIntrstAmount) {
        this.dcbbalIntrstAmount = dcbbalIntrstAmount;
    }


    //bi-directional many-to-one association to DemandLedgerMap
    @ManyToOne(fetch=FetchType.LAZY, cascade = { CascadeType.ALL })
    @JoinColumn(name="DCBBAL_DLDGR_MAP_ID")
    public DemandLedgerMap getDemandLedgerMap() {
        return this.demandLedgerMap;
    }

    public void setDemandLedgerMap(DemandLedgerMap demandLedgerMap) {
        this.demandLedgerMap = demandLedgerMap;
    }


    //bi-directional many-to-one association to DledgerApplicableMap
    @ManyToOne(fetch=FetchType.LAZY, cascade = { CascadeType.ALL })
    @JoinColumn(name="DCBBAL_DLAMAP_ID")
    public DledgerApplicableMap getDledgerApplicableMap() {
        return this.dledgerApplicableMap;
    }

    public void setDledgerApplicableMap(DledgerApplicableMap dledgerApplicableMap) {
        this.dledgerApplicableMap = dledgerApplicableMap;
    }

}

以下是DemandLedgerMap类

    import java.io.Serializable;
    import java.util.List;

    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.NamedQuery;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;

    import org.hibernate.annotations.Cascade;

    import com.mad.app.common.domain.BusinessEntity;


    /**
     * The persistent class for the demand_ledger_map database table.
     * 
     */
    @Entity
    @Table(name="demand_ledger_map")
    @NamedQuery(name="DemandLedgerMap.findAll", query="SELECT d FROM DemandLedgerMap d")
    public class DemandLedgerMap extends BusinessEntity implements Serializable {
        private static final long serialVersionUID = 1L;
        private int dldgrMapId;
        private byte dldgrMapRowStatus;
        private List<DcbLine> dcbLines;
        private DemandLedger demandLedger;
        private UnitAllocation unitAllocation;
        private List<DcbDailyBalance> dcbDailyBalances;

        public DemandLedgerMap() {
        }


        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="DLDGR_MAP_ID", unique=true, nullable=false, columnDefinition = "TINYINT")
        public int getDldgrMapId() {
            return this.dldgrMapId;
        }

        public void setDldgrMapId(int dldgrMapId) {
            this.dldgrMapId = dldgrMapId;
        }


        @Column(name="DLDGR_MAP_ROW_STATUS", nullable=false)
        public byte getDldgrMapRowStatus() {
            return this.dldgrMapRowStatus;
        }

        public void setDldgrMapRowStatus(byte dldgrMapRowStatus) {
            this.dldgrMapRowStatus = dldgrMapRowStatus;
        }


        //bi-directional many-to-one association to DcbLine
        @OneToMany(mappedBy="demandLedgerMap")
        public List<DcbLine> getDcbLines() {
            return this.dcbLines;
        }

        public void setDcbLines(List<DcbLine> dcbLines) {
            this.dcbLines = dcbLines;
        }

        public DcbLine addDcbLine(DcbLine dcbLine) {
            getDcbLines().add(dcbLine);
            dcbLine.setDemandLedgerMap(this);

            return dcbLine;
        }

        public DcbLine removeDcbLine(DcbLine dcbLine) {
            getDcbLines().remove(dcbLine);
            dcbLine.setDemandLedgerMap(null);

            return dcbLine;
        }


        //bi-directional many-to-one association to DemandLedger
        @ManyToOne(fetch=FetchType.LAZY, cascade = { CascadeType.ALL })
        @JoinColumn(name="DLDGR_MAP_DLDGR_ID", nullable=false)
        public DemandLedger getDemandLedger() {
            return this.demandLedger;
        }

        public void setDemandLedger(DemandLedger demandLedger) {
            this.demandLedger = demandLedger;
        }


        //bi-directional many-to-one association to UnitAllocation
        @ManyToOne(fetch=FetchType.LAZY, cascade = { CascadeType.ALL })
        @JoinColumn(name="DLDGR_MAP_UALLOC_ID", nullable=false)
        public UnitAllocation getUnitAllocation() {
            return this.unitAllocation;
        }

        public void setUnitAllocation(UnitAllocation unitAllocation) {
            this.unitAllocation = unitAllocation;
        }

        //bi-directional many-to-one association to DcbDailyBalance
            @OneToMany(mappedBy="demandLedgerMap")
            @Cascade(org.hibernate.annotations.CascadeType.ALL)
            public List<DcbDailyBalance> getDcbDailyBalances() {
                return this.dcbDailyBalances;
            }

            public void setDcbDailyBalances(List<DcbDailyBalance> dcbDailyBalances) {
                this.dcbDailyBalances = dcbDailyBalances;
            }

            public DcbDailyBalance addDcbDailyBalance(DcbDailyBalance dcbDailyBalance) {
                getDcbDailyBalances().add(dcbDailyBalance);
                dcbDailyBalance.setDemandLedgerMap(this);

                return dcbDailyBalance;
            }

            public DcbDailyBalance removeDcbDailyBalance(DcbDailyBalance dcbDailyBalance) {
                getDcbDailyBalances().remove(dcbDailyBalance);
                dcbDailyBalance.setDemandLedgerMap(null);

                return dcbDailyBalance;
            }

    }

请有人帮我解决这个问题

3 个答案:

答案 0 :(得分:0)

  

PropertyValueException:not-null属性引用null或   瞬态值:com.kssidc.app.domain.DemandLedgerMap.demandLedger

这清楚地表明,在类DemandLedgerMap中,您试图保留demandLedger,这是一个不可为空的字段。 因此,当您使用dcbDailyBalanceService.quartsTestdDisplay();时,您没有提供此值。

因此,在持久化/更新它时,它将为demandLedger查找一个值,并且它将获得一个空值,因此它会引发异常。

答案 1 :(得分:0)

您正在尝试保留DcbDailyBalance班级DemandLedgerMap。但是在DemandLedgerMap中,dldgrMapRowStatusdemandLedger列设置为nullable=false,并且您没有为此传递值。

这就是为什么当你试图坚持DcbDailyBalance时,它会抛出异常。希望它清楚。

答案 2 :(得分:0)

您需要将“dcbDailyBalance”添加到demandLedgerMap列表,然后插入demandLedgerMap。当您尝试更新dcbDailyBalance时,它会尝试保存dcbDailyBalance,然后“级联”并保存demandLedgerMap,因此demandLedgerMap“为null或瞬态值”。保存demandLedgerMap将创建和demandLedgerMap.id并且它不再是瞬态的,那么你可以保存dcbDailyBalance。试试这个:

demandLedgerMap.addaddDcbDailyBalance(dcbDailyBalance);
demandLedgerMapDAO.update(dailyBalanceDao);