title: org.hibernate.PropertyValueException: not-null property references a null or transient value : exception only with @Scheduled
body: <p>I am coding for a JPA project (hibernate with spring).</p>
  
  <p>The problem am facing is when I execute the below <code>interestCalculation()</code> method with <code>@Scheduled</code> annotation I am getting exception:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>After the <code>for</code> loop in <code>quartsTestdDisplay()</code> is completely executed. But, when I execute same method without scheduled data is persisting/updating after <code>for</code> loop is executed.
  Only when the method triggered from <code>@Scheduled</code> it is throwing exception.</p>
  
  <p>Below is my code, one is deligate layer class another one is service layer class.
  In delegate layer I have used <code>@Scheduled</code> for schedule trigger <code>interestCalculation()</code>.</p>
  
  <pre><code>  @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 &lt; 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);
  
       }
      }
  </code></pre>
  
  <p>Here's the exception:</p>
  
  <pre><code>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(&lt;generated&gt;) ~[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
  </code></pre>
  
  <p>Below is DcbDailyBalance class </p>
  
  <pre><code>    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;
      }
  
  }
  </code></pre>
  
  <p>Below is DemandLedgerMap class</p>
  
  <pre><code>    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&lt;DcbLine&gt; dcbLines;
          private DemandLedger demandLedger;
          private UnitAllocation unitAllocation;
          private List&lt;DcbDailyBalance&gt; 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&lt;DcbLine&gt; getDcbLines() {
              return this.dcbLines;
          }
  
          public void setDcbLines(List&lt;DcbLine&gt; 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&lt;DcbDailyBalance&gt; getDcbDailyBalances() {
                  return this.dcbDailyBalances;
              }
  
              public void setDcbDailyBalances(List&lt;DcbDailyBalance&gt; 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;
              }
  
      }
  </code></pre>
  
  <p>please anyone help me to resolve this problem</p>