-->
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: Newbie Mistake using saveOrUpdate?
PostPosted: Tue Aug 19, 2008 1:02 pm 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Hi All,

I think I am making a standard rookie Hibernate mistake when trying to update
some database information. Based on this assumption, I have left out
the implementation details but can provide them if needed.

Problem Scenario:
Class A has a list of Class B, using a typical bi-directional mapping.
Class B has a list of Class C, also using a typical bi-directional mapping.

An entire A-B-C object graph is read in using an HQL query.
An instance of Class C is searched for within the object graph.
One property with the Class C instance is changed.
The Hibernate session's saveOrUpdate() method is executed on the Class A
instance in order to update the database with the change made to the instance
of Class C. This entire sequence takes place within an Hibernate session, which is
provided through an implementation of the Open Session In View pattern.

The problem is that the database table record associated with the modified
Class C instance does not get updated.

Any help will be gratefully received.

Thanks,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 3:27 pm 
Newbie

Joined: Wed Oct 31, 2007 5:36 pm
Posts: 13
Sounds like a problem with the cascade rules being used. Will know more if you post your mapping or annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 4:16 pm 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Thanks for replying, kid1125!

Class A = EobPackage
Class B = Section
Class C = Step


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="EobPackage" table="PACKAGE">

<id name="id" column="ID" type="int">
<generator class="native"/>
</id>

<property name="createdBy" column="CREATED_BY" type="string" />
<property name="dateCreated" column="DATE_CREATED" type="date" />
<property name="dateLastModified" column="DATE_LAST_MODIFIED" type="date" />
<property name="status" column="STATUS" type="string" />
<property name="title" column="TITLE" type="string" />

<many-to-one class="Applicant" name="applicant" column="APPLICANT_ID" />

<list name="sections" inverse="true" cascade="save-update" >
<key column="PACKAGE_ID" />
<list-index column="ORDER" />
<one-to-many class="Section" />
</list>

</class>

</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="Section" table="SECTION">

<id name="id" column="ID" type="int">
<generator class="native"/>
</id>

<property name="description" column="DESCRIPTION" type="string" />
<property name="order" column="ORDER" type="int" />
<property name="title" column="TITLE" type="string" />

<many-to-one name="eobPackage" class="EobPackage" column="PACKAGE_ID" not-null="true"/>

<list name="steps" inverse="true" cascade="save-update" >
<key column="SECTION_ID" />
<list-index column="ORDER" />
<one-to-many class="Step" />
</list>

</class>

</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="Step" table="STEP">

<id name="id" column="ID" type="int">
<generator class="native"/>
</id>

<property name="description" column="DESCRIPTION" type="string" />
<property name="form" column="FORM" type="string" />
<property name="order" column="ORDER" type="int" />
<property name="required" column="REQUIRED" type="boolean" />
<property name="signatureRequired" column="SIGNATURE_REQUIRED" type="boolean" />
<property name="status" column="STATUS" type="status" not-null="true" update="false" access="field"/>
<property name="title" column="TITLE" type="string" />

<many-to-one name="section" class="Section" column="SECTION_ID" not-null="true"/>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 5:22 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate implements automatic dirty checking which means that if you just change property values the changes will be written back to the database. You do not have to call Session.saveOrUpdate(). You do, however be aware of transactions, unit-of-work, etc. Most importantly, you must commit your transaction to make sure changes are written to the database. You can find a lot of information about this on the Hibernate wiki pages, http://www.hibernate.org/37.html.

This is just a guess. If it still doesn't work, please post relevant parts of the code as well.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 6:55 pm 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Re-reading Chapter 11...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 8:22 pm 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Doh doh doh!

I just spotted the update="false" attribute in the status property of the
Step mapping -- it leaked in from some sample code I was working
from.

All works now -- thanks for all your help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 8:25 pm 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Doh doh doh!

I just spotted the update="false" attribute in the status property of the
Step mapping -- it leaked in from some sample code I was working
from.

All works now -- thanks for all your help.


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.