I'm playing around with versioning since a week. The row will be updated once and only once after that is start throwing StalObjectStateException. Please help !!!
Here's the code.
hibernate mappingCode:
<hibernate-mapping>
<class name="com.xxx.loan.CompositeLoan" table="LOAN"
schema="ABC" lazy="false">
<id name="LoanId" type="long">
<column name="ID_LOAN" />
<generator class="assigned" />
</id>
<version column="DT_LST_UPDT" name="dtLstUpdt" type="XXTimeStampType" generated="always"/>
...........
java codeCode:
public static Session session = HibernateUtil.getSessionFactory().openSession();
public static void main(String[] args) {
Transaction tx = null;
try {
tx = session.beginTransaction();
TestProgram test = new TestProgram();
long loanID = 812814486;
try{
CompositeLoan loan2 = test.updateCompositeLoan(loanID);// call the method (below)
session.save(loan2);
session.getTransaction().commit();
....
session.close();
//method to be called from above
public CompositeLoan updateCompositeLoan(long loanID)throws Exception
{
CompositeLoan loan = null;
try
{
loan = (CompositeLoan)session.load(CompositeLoan.class, loanID);
loan.setxxx(Boolean.TRUE);
final Calendar ddlpiDate = Calendar.getInstance();
ddlpiDate.add(Calendar.MONTH, -1);
ddlpiDate.set(Calendar.DAY_OF_MONTH, 15);
loan.setxxxxxDate(new XXTimeStampType(ddlpiDate.getTime()));
loan.setDtLstUpdt(new XXTimeStampType(System.currentTimeMillis()));
..............
getting the following error
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.xxx.loan.CompositeLoan#812814486]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1714)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2357)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2257)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2557)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.xxx.yyy.TestProgram.main(TestProgram.java:98)