-->
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.  [ 8 posts ] 
Author Message
 Post subject: deep copy and save of object graph
PostPosted: Tue Apr 22, 2008 7:41 pm 
Newbie

Joined: Fri Dec 22, 2006 12:35 am
Posts: 5
hi,
i am searching for a simple solution to deep copy an object and then save it into the DB. i could not find any working solution out there.
the scenario is as follows:
1. load an object from the DB
2. deepCopy the object graph (an entity with one to one /many to one associations as well as a set) i tried serializations, beanLib and beanUtil copyProperties, they all do the job
3.change some of the information in the graph (not only on top level)
4.save back to the DB creating inserts on the new Entities.

i tired several ways, and the most i got is that the top most object in the graph was saved but not the underlying changes

the only way i was able to make it work manually was to persist the object bottom up (by calling save on the transient objects).

i am using hibernate3 and Spring
Thanks,
Yaron


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 12:45 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Check if you have the cascade property set to save-update in your mappings.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 12:58 am 
Newbie

Joined: Fri Dec 22, 2006 12:35 am
Posts: 5
Hi Sukirtha,
the sets that i have are cascasde="all-delete-orphan"
here are the mappings of the top most entity:
fpr simplicity i took out the primitive properties. left the entity TuitSchoolCalTerm (which in turns calls other entites) and one of the sets
Code:
<hibernate-mapping>
    <class name="TuitReq" discriminator-value="0" table="TUIT_REQ">
        <id name="id" type="java.lang.Long">
            <column name="ID" scale="0" />
            <generator class="native" />
        </id>
       <discriminator column="TUIT_REQ_TYPE" type="java.lang.Integer"/>       
         <property name="tuitSchoolCalTerm" type="TuitSchoolCalTerm">
            <column name="SCHOOL_CAL_TERM" scale="0" not-null="true" />
        </property>   
       
        <property name="empId" type="java.lang.Long">
            <column name="EMP_ID" scale="0" not-null="true" />
        </property>       
        .... more properties....
        <subclass name="TuitReqEmp" discriminator-value="1">
           .... more properties....      
        <set name="tuitCareerRels" inverse="true" cascade="all-delete-orphan" outer-join="true" lazy="true">
            <key>
                 <column name="REQ_ID"/>               
            </key>
            <one-to-many class="TuitCareerRel" />
        </set>
          
        </subclass>
        <subclass name="TuitReqDep" discriminator-value="2">
        .... more properties....
           
        </subclass>
       
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 1:15 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
cascade="all-delete-orphan" should be replaced with cascade="all, delete-orphan". The values have to be comma seperated and not hyphen seperated. Hope it is not a typo.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 1:29 am 
Newbie

Joined: Fri Dec 22, 2006 12:35 am
Posts: 5
Thanks Sukirtha,
cascade="all-delete-orphan" are correct and do work, in any case this is not the problem
(as even without the sets it does not save correctly).
Thanks,
Yaron


Top
 Profile  
 
 Post subject: Deep copy - clear IDs
PostPosted: Mon Dec 15, 2008 7:39 pm 
Newbie

Joined: Mon Dec 15, 2008 7:36 pm
Posts: 2
Hi.
When you made your deep copy, the new POJOs would have the IDs of the original entities. did you find a nice way to clear these so that hibernate will insert them rather than update existing entities?
Thanks,
Matt.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 8:29 pm 
Newbie

Joined: Fri Dec 22, 2006 12:35 am
Posts: 5
Hi Matt,
that was not my purpose, so no, i didn't.
i would assume you would have to evict all the pojos and remove the id's and then save.
maybe you can do it as part of the deepCopy code for each object?
good luck


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 8:55 pm 
Newbie

Joined: Mon Dec 15, 2008 7:36 pm
Posts: 2
In my case I'm storing "definition" entities in the DB that are used as the basis for stuff that will be modified at runtime on a per-user basis, then saved for that user as state.

I've made a deep copy of my pojos which appears to be fine.

I was hoping that session.save would force an insert (ignore the id), but alas, no according to the documentation.

I'm not wanting to go through all beans/associates and call setId(null). that would be pretty lame.


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