-->
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.  [ 12 posts ] 
Author Message
 Post subject: <version> tag problem.
PostPosted: Mon Dec 22, 2003 4:04 pm 
Newbie

Joined: Mon Dec 22, 2003 3:34 pm
Posts: 6
Location: USA
Hi there,

We are using <version> tag to implement automatic versioning but not having much luck with session.saveOrUpdate(). When we do NOT use <version> tag in our Cat.hbm.xml, everything works fine and the record is updated in the database.

But when we use <version> tag, record in the database will NOT be updated. We neither see any 'update' statement being executed nor any errors in the log file.

Here is the Cat.hbm.xml file:

<hibernate-mapping>
<class name="com.genco.cat.object.Cat" table="CAT" dynamic-update="true">
<id name="id" type="string">
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<version name="version" column="version"></version>

<property name="name" type="string">
<column name="name" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="sex" type="string"/>
<property name="weight" type="float"/>
</class>
</hibernate-mapping>

Do we need to use 'unsaved-value' attribute for the <version> tag? If yes, how? When we use this attribute, I get "unsaved-value used but not declared" error.

Here is the sinppet of the code we are using to update Cat in the session bean:

public Cat update(Cat cat) throws HibernateException {
// we get the SessionFactory in setSessionContext() method
Session session = sessionFactory.openSession();
try {
session.saveOrUpdate(cat);
session.flush();
}
catch (HibernateException e) {
e.printStackTrace();
sesContext.setRollbackOnly();
throw e;
}
finally {
session.close();
}
return cat;
}

We tried all the possible combinations and searched through forums. I am sure we are missing someting here. If anyone of you can let us know how to use <version> tag for automatic versioning or point us in the right direction or give us an example on how to use it correctly, we would really appreciate it.

Thank you for your time.
Prasad.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 7:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
unsaved-value attribute of version is optional. Default value will let the id unsaved-value be used.
Using version will either :
- properly update your DB
- throw a StaleObjectException.

Double check your exceptions, your tx, your executed statements

version unsaved-value can be :
- undefined (default value)
- null (insert if version is null, update otherwise
- negative (insert if version is negative or null, update otherwise) : useful for primitive typed version

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2004 6:52 pm 
Newbie

Joined: Mon Dec 22, 2003 3:34 pm
Posts: 6
Location: USA
Thanks Emmanuel for your help.

Now I could update the record once without a problem. But when I try to update the same record again, I get the following error:

*********************
17:36:10,183 ERROR StaleObjectStateException:27 - An operation failed due to stale data
net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for com.genco.xxx.component.Cat instance with identifier: 402880e5fa01e9f000fa01ec92830001 at net.sf.hibernate.persister.AbstractEntityPersister.check(AbstractEnti
tyPersister.java:505)
*********************

I use the following <id> and <version> tags in Cat.hbm.xml:

<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<version name="version" type="long" column="version"></version>

Am I missing something? I would really appreciate any help.

Thanks,
Prasad.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2004 7:06 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you really doing the two updates directly after each other? I can't reproduce this, please show your code.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 12:15 am 
Newbie

Joined: Mon Dec 22, 2003 3:34 pm
Posts: 6
Location: USA
Thanks gloeglm for the quick response.

Let me explain the problem.

I am using Session Bean, struts, OC4J server, Oracle 9i database, Container Managed Transactions.

Here is the work flow.

JSP -> Struts Action class -> Session Bean (Hibernate stuff is done here).

I added cat details using the form (jsp). On submit, entered cat details into the database and displayed cat details back on the screen. Then I updated cat details, no errors. In the database, cat details are updated and version column value changes from 0 to 1 automatically. Updated cat details are displayed on the screen.

When I update cat details again and submit, I get the following error in the console.

***********************
04/01/10 22:43:36 Hibernate: update CAT set version=?, name=?, sex=?, weight=? where id=? and version=?
22:43:36,838 ERROR StaleObjectStateException:27 - An operation failed due to stale data
net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for com.genco.xxx.component.Cat instance with identifier: 402880e5fa03060200fa0306060c0001
at net.sf.hibernate.persister.AbstractEntityPersister.check(AbstractEntityPersister.java:505)
***********************

Here is the code snippet to update cat in Session Bean:

public Cat updateCat(Cat aCat) throws HibernateException {

Session session = null;
try {
session = sessionFactory.openSession();
session.saveOrUpdate(aCat);

session.flush();
}
catch (HibernateException e) {
throw e;
}
finally {
session.close();
}
return aCat;
}

Code snippet from Cat.hbm.xml file:
********************************
<hibernate-mapping>

<!--
Set dynamic-update=true, to use optimistic locking with automatic versioning.
-->
<class name="com.genco.xxx.component.Cat" table="CAT" dynamic-update="true">
<!--
unsaved-value (optional - defaults to null):
An identifier property value that indicates that an instance is
newly instantiated (unsaved), distinguishing it from transient
instances that were saved or loaded in a previous session.
-->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<version name="version" type="long" column="version"></version>

<property name="name" type="string">
<column name="name" sql-type="varchar(16)" not-null="true"/>
</property>

<property name="sex" type="string"/>

<property name="weight" type="float"/>

</class>

</hibernate-mapping>
********************************

The error says "Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)".

By using Container managed transactions (CMT), transaction would be commited when I updated the cat first time. Container starts new trasaction when I try to update the cat second time. Is anything wrong here?

I did not set unsaved-value attribute for the version tag. Since it is undefined, according the reference mannual, it should use the identifier property value (<id>). Am I doing anything wrong here?

I set <class> attribute dynamic-update="true" to enable versioning.

Sorry for posting a long message.

I really appreciate any help.

Thanks in advance.

Prasad.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 10:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
That should really work. Are you really using the Cat object returned by your EJB method for the second update?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 2:31 pm 
Newbie

Joined: Mon Dec 22, 2003 3:34 pm
Posts: 6
Location: USA
gloeglm wrote:
That should really work. Are you really using the Cat object returned by your EJB method for the second update?


Yes. I am using the same Cat object returned by the EJB. After updating second time, I am sending the Cat to the same EJB for update.

Any ideas???

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 2:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No idea - use your debugger to check if the version property is correctly incremented for your returned Cat


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 8:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Where aCat come from. If it come from an other EJB call, be sure to call session.flush() before returning it. Hibernate will set version and update it when flushing.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 11:59 am 
Newbie

Joined: Mon Dec 22, 2003 3:34 pm
Posts: 6
Location: USA
Thanks for your reply, Emmanuel.

aCat comes to EJB's updateCat(Cat aCat) method from struts action class (from the jsp). (updateCat(Cat aCat) is called directly from struts action class) It does not come from anothr EJB call.

It updates the cat first time without any problem. Then I retrieve the cat and display it on the jsp screen, upate and submit then it throws the error.

My question is, how it worked first time when I update it? I use the same code to do the same thing and it fails.

I would really appreciate any help.

Thanks in advance,
Prasad.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 12:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Where 'cat' come from in your struts action class ? It has to come from db since you updated it.

1. Get Cat from DB with version=2
2. update Cat with version=2 ; when updated, Hibernate increase version to 3
3. update a staled copy of cat (with version=2) ; when updated, Hibernate raise a StaleObjectException, it expects version=3

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 4:59 pm 
Newbie

Joined: Mon Dec 22, 2003 3:34 pm
Posts: 6
Location: USA
Thanks for your help Emmanuel and gloeglm.

It is working now. Our user interface was initializing the version value to be '0' everytime I submit the data. It updated fine first time becoz the version value in the databae and the value coming from the form are same (ie; 0). But the next time I try to update, version values are different (DB value = 1 and jsp value=0). That was the reason it failed to update the record.

Thanks,
Prasad.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.