-->
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: Hibernate Versioning and the Detached Objects
PostPosted: Tue Feb 07, 2006 6:11 am 
Beginner
Beginner

Joined: Mon Dec 05, 2005 4:15 am
Posts: 36
I am trying to implement the optimistic lock in my web application. I want to make this without <version> column, with the automatic versioning.

The Hibernate Reference says, "The application manipulates the state of detached instances originally
loaded in another Session and then reattaches them using Session.update(), Session.
saveOrUpdate(), or Session.merge().
// foo is an instance loaded by a previous Session
foo.setProperty("bar");
session = factory.openSession();
Transaction t = session.beginTransaction();
session.saveOrUpdate(foo); // Use merge() if "foo" might have been loaded already
t.commit();
session.close();
Again, Hibernate will check instance versions during flush, throwing an exception if conflicting updates occured."


Here is my code:

EditServlet, will be executed when the user want to edit a data row.

...
SessionFactory sf = HUtils.getSf();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Adressen a = (Adressen)s.get(Adressen.class,request.getParameter("KundenNr"));
tx.commit();
s.close();
req.getSession().setAttribute("adresse",a);
req.getRequestDispatcher("edit.jsp").forward(req,res);
...

I get the Object with the given ID, detach it from the Hibernate Session and store this POJO in the HTTP Session.

SaveServlet, will be executed when the user has edited the row and will store the change in the database:

...
Adressen a = (Adressen) req.getSession().getAttribute("adresse");
a.setHausnr(req.getParameter("hausnr"));
a.setName1(req.getParameter("name1"));
a.setName2(req.getParameter("name2"));
a.setStrasse(req.getParameter("strasse"));
a.setPLZ(req.getParameter("plz"));
a.setOrt(req.getParameter("ort"));
SessionFactory sf = HUtils.getSf();
Session s = sf.openSession();
Transaction t = s.beginTransaction();
s.update(a);
t.commit();
s.close();
...

if 2 user begin simultaneously edit the same row, the second user overwrite the changes made by the first user. The StaleObjectStateException will not be throwed.

What I do wrong?

here my POJO and my Mapping:

POJO:

public class Adressen {

private String KundenNr;
private String name1;
private String name2;
private String strasse;
private String hausnr;
private String PLZ;
private String ort;

//+ all Getters and Setters

}

Mapping:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="mypack.Adressen" table="Adressen">
<id name="KundenNr">
<generator class="assigned"/>
</id>
<property name="name1"/>
<property name="name2"/>
<property name="strasse"/>
<property name="hausnr"/>
<property name="PLZ"/>
<property name="ort"/>
</class>
</hibernate-mapping>

Thanx for the answers.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 9:35 am 
Newbie

Joined: Wed Jan 04, 2006 9:13 am
Posts: 10
Location: Ukraine, Odessa
after <id> tag you may add some as:
<version name="version" column="version" type="int"/>

in <class> specify optimistic-lock="version"

also you need to add in you table version column

and getters/setters for this atribute in POJO

а .іс.. ..о.о .і.т. ку..т. .орі.к. та .р.с.ат. її .ану ..енко


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 12:24 pm 
Regular
Regular

Joined: Tue May 03, 2005 8:19 am
Posts: 53
Location: Paris
I have the same problem with version 3.0.5 et 3.1.2.

My french Post with a junit test.

Translation (Thanks Thierry) :

I’m trying to use the automatic versioning in detached mode as described in chapter 11.3.3.
(http://www.hibernate.org/hib_docs/v3/re ... c-detached)

Here’s my problem:

When I create a new person, the version is set to 0.
During a save, it is set to 1.
Everything is normal so far.
If I try to save the object with a version set to 0, I expect to get an exception, but I don’t get it! Nevertheless no change is performed in the database.


Watching the SQL code, I realize it tries to update a non-existent row, but I’m not informed there has not been any modification
update Person set version=1 where name='Gavin' and version=0

I certainly forgot a parameter, but which one???

To illustrate the case, I added the following test method in the class org.hibernate.test.version.VersionTest


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 12:41 pm 
Newbie

Joined: Wed Jan 04, 2006 9:13 am
Posts: 10
Location: Ukraine, Odessa
lauvigne wrote:
I have the same problem with version 3.0.5 et 3.1.2.

My french Post with a junit test.

Translation (Thanks Thierry) :

I’m trying to use the automatic versioning in detached mode as described in chapter 11.3.3.
(http://www.hibernate.org/hib_docs/v3/re ... c-detached)

Here’s my problem:

When I create a new person, the version is set to 0.
During a save, it is set to 1.
Everything is normal so far.
If I try to save the object with a version set to 0, I expect to get an exception, but I don’t get it! Nevertheless no change is performed in the database.


Watching the SQL code, I realize it tries to update a non-existent row, but I’m not informed there has not been any modification
update Person set version=1 where name='Gavin' and version=0

I certainly forgot a parameter, but which one???

To illustrate the case, I added the following test method in the class org.hibernate.test.version.VersionTest


you don't have same problem.

.і... ува.н.м тре.а .ут. х.о...ку
в те.е нема .ро..ем. .ка є у автора


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 3:51 pm 
Regular
Regular

Joined: Tue May 03, 2005 8:19 am
Posts: 53
Location: Paris
Why it is not the same problem?

In my mapping, I have the element version.
In my table version column
and getters/setters for this attribute in POJO.

But no Exception if conflicting updates occured.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 3:43 am 
Newbie

Joined: Wed Jan 04, 2006 9:13 am
Posts: 10
Location: Ukraine, Odessa
lauvigne wrote:
Why it is not the same problem?

In my mapping, I have the element version.
In my table version column
and getters/setters for this attribute in POJO.

But no Exception if conflicting updates occured.


Because author of topic don't map versionning at all.

See more closely to his maping and POJO.
His maping does not have tag <version> and other needed things.
Hibernate by default (if not specify in <class>) use optimistic-lock="version".
How hibernate can know about versionning if in mapping no <version> tag?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 1:51 pm 
Regular
Regular

Joined: Tue May 03, 2005 8:19 am
Posts: 53
Location: Paris
OK, it is not the same problem!

My problem is related to my driver JDBC oracle which return -2 instead of the good number of line modified.


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.