-->
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.  [ 3 posts ] 
Author Message
 Post subject: Re-persisting a detached object in a web app
PostPosted: Thu Mar 30, 2006 3:25 pm 
Newbie

Joined: Thu Mar 30, 2006 3:08 pm
Posts: 2
I have a graph of objects ( a parent child relationship) that the user edits through a web screen. The objects are fetched through hibernate, and the session is closed, thus detaching the objects. After the user edits the objects, I re-persist the objects by calling session.update. When simulating this when testing my DAO, when I add children, I can see that the sql executed is that adding the new children. When I test my web app, I can see that the sql executed is to update all the children objects along with adding the new ones.

Can someone explain why in one test, the sql generated is only for the new objects (which is what I want) and in the other sql is generated for all the objects?

I am using Hibernate version:2.1.7 developing using IBM Websphere Studio version 5.1.0 using java 1.3.

Thanks in advance.


Top
 Profile  
 
 Post subject: parent.saveOrUpdate - saves graph w/ detached objects
PostPosted: Thu Mar 30, 2006 5:40 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
I can only comment on hibernate 3.. but if the behavior was the same, then it should help you...

1) save vs saveOrUpdate
save(...) will always try to insert a new object.
you should be using saveOrUpdate()...

2) You should be using the attribute on your <ID> of unsaved-value="null".
This way when you supply an ID hibernate knows that it will be an Update vs Insert (null for insert).

3) in your Parent.children mapping (ie, <set>, put inverse="false", since you'd like the object links to be managed by the parent, and also put cascade="all-delete-orphan". Then save/update operations will be propogated to your child objects.

4) In your java code, do NOT do a merge, but rather only create a transaction, and saveOrUpdate(parent). and commit()...
the entire graph will be saved correctly, including additions/deletions to your child set; and any updates to the children themselves.

Make sure your FK in your child table are nullable (and as a caveate, even though you are specifying all-delete-orphan, I find that the orphans are not deleted, just the FKs set to null).

Hope this helps!

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 10:28 am 
Newbie

Joined: Thu Mar 30, 2006 3:08 pm
Posts: 2
Thanks for your input, unfortunately it wasn't able to help. I did switch the call from update to saveOrUpdate, but that did not chage the behavior. The id is a natural key, so having unsaved-value="null" will not help. My mappings are set up along the manner you stated. Here they are below. Perhaps you can see something I've missed.

Code:
    <class name="com.aegon.us.afp.awdat.dao.model.AWDBizUnit" table="AWD_BIZ_UNIT_REF">
        <id name="cd" column="AWD_BIZ_UNIT_CD" type="string">           
        <generator class="assigned"/>
        </id>
        <timestamp name="lastUpdateDate" column="RCD_LST_MOD_TS" unsaved-value="null"/>
        <property name="desc" type="string" column="AWD_BIZ_UNIT_DSC"/>
        <property name="emailOutOfNetwork" type="string" column="ALW_EMAIL_OUT_OF_NTWK_IND"/>       
        <property name="statusCd" type="string" column="RCD_STS_CD"/>       
        <property name="createUser" type="string" column="RCD_CRT_USER"/>
        <property name="createDate" type="timestamp" column="RCD_CRT_TS"/>       
        <property name="lastUpdateUser" type="string" column="RCD_LST_MOD_USER"/>
      <property name="awdRgnPrtNbr" type="integer" column="AWD_RGN_PRT_NBR" />
      <property name="awdRgnName" type="string" column="AWD_RGN_NM" update="false" insert="false" />
      
        <set name="outboundValidSoures" table="OTBND_VLD_SRC" lazy="false" cascade="all-delete-orphan" inverse="true">
           <key column="AWD_BIZ_UNIT_CD"/>
           <one-to-many class="com.aegon.us.afp.awdat.dao.model.OutboundValidSource" />
        </set>
                     
    </class> 


    <class name="com.aegon.us.afp.awdat.dao.model.OutboundValidSource" table="OTBND_VLD_SRC">
        <composite-id >
           <key-property name="awdBusUnitCd" column="AWD_BIZ_UNIT_CD" type="string"/>           
           <key-property name="awdSourceCd" type="string" column="AWD_SRC_CD"/>
        </composite-id >
        <timestamp name="lastUpdateDate" column="RCD_LST_MOD_TS" unsaved-value="null"/>
        <property name="createUser" type="string" column="RCD_CRT_USER"/>
        <property name="createDate" type="timestamp" column="RCD_CRT_TS"/>       
        <property name="lastUpdateUser" type="string" column="RCD_LST_MOD_USER"/>       
    </class> 





I do create a transaction and commit as a part of my call. As far as making the FK in the child table nullable. That is outside my ability. The FK also makes up part of the primary key. Besides, its a legacy system and its design is beyond my control.

I hope you see something. This is the first project we have using Hibernate. It relatively simple, but we have a much larger one coming up with some very complex graphs of objects. I would like to take advantage of hibernates ability to determine what is dirty an what is not to save on IO.

Thanks


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