'org.hibernate.PropertyValueException: not-null property references a null or transient value : exception only with @Scheduled
I am coding for a JPA project (hibernate with spring).
The problem am facing is when I execute the below interestCalculation() method with @Scheduled annotation I am getting exception:
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
After the for loop in quartsTestdDisplay() is completely executed. But, when I execute same method without scheduled data is persisting/updating after for loop is executed.
Only when the method triggered from @Scheduled it is throwing exception.
Below is my code, one is deligate layer class another one is service layer class.
In delegate layer I have used @Scheduled for schedule trigger 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);
}
}
Here's the exception:
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
Below is DcbDailyBalance class
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;
}
}
Below is DemandLedgerMap class
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;
}
}
please anyone help me to resolve this problem
Solution 1:[1]
You are trying to persist DcbDailyBalance class which has DemandLedgerMap class. But in DemandLedgerMap, dldgrMapRowStatus and demandLedger columns are set to nullable=false and you are not passing value for this.
That's why when you try to persist DcbDailyBalance, it is throwing exception. Hope it's clear.
Solution 2:[2]
You need to add you "dcbDailyBalance" to demandLedgerMap list and then insert demandLedgerMap. When you try to update dcbDailyBalance, it'll try to save dcbDailyBalance and then "cascade" and save demandLedgerMap, therefore demandLedgerMap "is null or transient value". Saving demandLedgerMap will create and demandLedgerMap.id and it won't be transient anymore, then you can save dcbDailyBalance. Try this:
demandLedgerMap.addaddDcbDailyBalance(dcbDailyBalance);
demandLedgerMapDAO.update(dailyBalanceDao);
Solution 3:[3]
PropertyValueException: not-null property references a null or transient value : com.kssidc.app.domain.DemandLedgerMap.demandLedger
This clearly states that in the class DemandLedgerMap you are trying to persist demandLedger, which is a non-nullable field.
So when you are using dcbDailyBalanceService.quartsTestdDisplay(); you are not providing the value for this.
Thus while persisting/updating this it will look for a value for demandLedger and it is getting a null value thus it throws an exception.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vimal Bera |
| Solution 2 | Sant |
| Solution 3 | ono2012 |
