-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Can't get versioning to work
PostPosted: Tue Nov 01, 2005 12:05 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 3:07 am
Posts: 33
Location: Sydney
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

I'm trying to implement optimistic-locking. I have a version colum and an int property in my Customer object. I create a customer, id is set and version is set to 0. Fine. I then update the customer -version is set to 1. Also fine. Any other interactions with the database, even just fetches, result in the version being incremented. The log file shows that hibernate is picking the version property as "dirty" and goes on to update it. I am not modifying any property of the customer in these operations. Any subsequent attempts to update the customer result in a stale object exception.

Hibernate version:3.1

Mapping documents:
<class name="com.wikidsistas.model.Customer"
table="customer"
optimistic-lock="version">
<cache usage="read-write"/>
<id name="custId" column="cust_id" type="java.lang.Long" unsaved-value="null">
<generator class="increment"/>
</id>
<version name="version" column="version" unsaved-value="null"/>
<property name="title" type="string" column="title"/>
<property name="firstName" type="string" column="first_name"/>
<property name="surname" type="string" column="surname"/>
<property name="subscribed" type="boolean" column="subscribed"/>
<property name="emailAddress" type="string" column="email_address"/>
<property name="mobilePhone" type="string" column="phone_mobile"/>
<property name="phoneOther" type="string" column="phone_other"/>
<component name="address" class="com.wikidsistas.model.Address">
<property name="addressLine1" type="string" column="address_line1"/>
<property name="addressLine2" type="string" column="address_line2"/>
<property name="suburb" type="string" column="suburb"/>
<property name="state" type="string" column="state"/>
<property name="postcode" type="integer" column="postcode"/>
</component>
</class>


org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.wikidsistas.model.Customer#1]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1634)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2207)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2117)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2373)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:84)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:877)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:345)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:490)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:495)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:468)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:258)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy1.updateCustomer(Unknown Source)
at com.wikidsistas.web.struts.customer.SaveCustomerChangesAction.execute(SaveCustomerChangesAction.java:59)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)


Name and version of the database you are using: MySQL 5 InnoDB


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 12:32 am 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
FYI I'm having this same problem: aggressive version incrementing when no updates are being made to the object.

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 12:48 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 3:07 am
Posts: 33
Location: Sydney
I've just discovered the problem. I have a version property in my customer class. The setter method I have is:

public void setVersion(int vers)
{
this.version = vers;
}

I was calling this.version = version by mistake.

Still coming across stale object exceptions when I try to update the object a second time though - not sure whats causing that.[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 12:55 am 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
Hmm, I doubled checked my code hoping for a quick fix. Unfortunately that isn't the case in my circumstance. Thanks for the info. I hope someone can respond to your exception issue.

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 12:58 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 3:07 am
Posts: 33
Location: Sydney
Check your log files to find out which property "is dirty". In my case it was the version property.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 1:16 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
You're right, I've got two columns showing up as dirty: version, and another column. The odd thing is that they are both very simple properties.. nothing odd about them, here's the version:

@Version
@Column(name="version")
@NotNull
public int getVersion() {
return version;
}
protected void setVersion(int version) {
this.version = version;
}

I'll keep digging deeper. This is obviously the problem, I just have to figure out why hibernate thinks my two columns are dirty.

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 1:33 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
Got it. Mine came down to the implementation of a custom UserType. Adding the following highlighted line fixed the problem:

public boolean equals(Object x, Object y) throws HibernateException {
** --> if (x == null && y == null) return true;
if (x== null || y == null) return false;
return x.equals(y);
}

In short, it wasn't treating nulls as equal. Thanks for your help on this.

Ryan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.