-->
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: Cloning/ copying advice
PostPosted: Wed Jan 14, 2009 10:48 am 
Newbie

Joined: Thu Jun 26, 2008 8:59 am
Posts: 9
Location: Dublin, Ireland
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2

Mapping documents:
NA

Code between sessionFactory.openSession() and session.close():
NA

Full stack trace of any exception that occurs:
NA

Name and version of the database you are using:
mysql 5.0.41

The generated SQL (show_sql=true):
NA

Debug level Hibernate log excerpt:
NA


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Hi guys,

I have a requirement that I'm finding hard to design an implementation for. The requirement is to be able to copy an existing object graph and save new one from the copy. What's making it quite difficult is that some of the Objects in the graph need to be saved as new objects but others need to remain as references to existing objects.

An analogy:

Clone an Order and add a new customer to it. I want a new Order object and a new Customer Object but I want all the references to product to remain as references to the products rather than create new ones.

Completely stuck on this one any help greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 11:06 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
After you you load an existing graph you could set all ids of the objects you wish to be created to 0 (or your equivalent value for unsaved objects). Then you save your root object. If you have cascading enabled in your graph (CascadeType.PERSIST), hibernate will insert the new ones.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 11:26 am 
Newbie

Joined: Thu Jun 26, 2008 8:59 am
Posts: 9
Location: Dublin, Ireland
Thanks for the quick reply. I had tried to change the Ids but hibernate threw and Exception because i was trying to change teh ID of an object still associated with a session.

It's given me an idea though, I'll try doing a deep copy then changing the IDs as you suggested. One thing I don't know how to do though is change the cascade type either for a specific Object or a specific time i.e. change it then change it back. Is this possible?


Thanks again :-)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 11:42 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Quote:
One thing I don't know how to do though is change the cascade type either for a specific Object or a specific time i.e. change it then change it back. Is this possible?


Sorry, I don't understand what you mean. Can you explain? Maybe you could post your mapping and your use-case.[/code][/url]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 11:49 am 
Newbie

Joined: Thu Jun 26, 2008 8:59 am
Posts: 9
Location: Dublin, Ireland
I'm actually using annotations so I assume I'm going to have to

Code:
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})


for each getter or relationship


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 11:52 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Yes, for example. Or CascadeType.PERSIST.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 1:27 pm 
Newbie

Joined: Thu Jun 26, 2008 8:59 am
Posts: 9
Location: Dublin, Ireland
Hmmmm,

So everything seems ok until my code tries to do a get that returns a Set. I have set the Cascade. When I debug and look at the Objects taht are referecned, everything that is a single Object is ok, but if it is a one to many the get returns the Set but it's empty.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 10:07 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
When you debug it is possibly empty, because it has not been loaded lazily, yet. Have you tried to invoke the getter-method? If you pasted your complete mapping and test-case it would be much easier to help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 11:18 am 
Newbie

Joined: Thu Jun 26, 2008 8:59 am
Posts: 9
Location: Dublin, Ireland
Setting the fetch type to eager on one to many relationships solved that problem. Now I have another one :-(. when I try to persist I get

Code:
org.springframework.orm.hibernate3.HibernateSystemException: Flush during cascade is dangerous; nested exception is org.hibernate.HibernateException: Flush during cascade is dangerous


No stack trace, which would help me a lot.
I've removed all session flushes from my save methods in my DAOs but I'm still getting the same problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 11:21 am 
Newbie

Joined: Thu Jun 26, 2008 8:59 am
Posts: 9
Location: Dublin, Ireland
Oh and I'm not deleting anything as it says here, http://www.hibernate.org/117.html#A26


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 5:43 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Did you set the ids to 0 after you detached the objects or before you did it? you should do it after detaching it.

Could you paste your code, where you try to copy your entities?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 5:55 am 
Newbie

Joined: Thu Jun 26, 2008 8:59 am
Posts: 9
Location: Dublin, Ireland
Code:
         long masterListId = new Long(orgId);
         orgMasterList = _masterListManager.getAt(masterListId);
//         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//         session.evict(orgMasterList);         
         
         masterList = (MasterList)SerializationHelper.clone(orgMasterList);      
         
         _masterListManager.clear();   
         
         
         masterList.setDescription("Clone");
         masterList.setStatus(WorkflowElement.Preview);
         
         masterList.setId(null);
         

         
         //_masterListManager.getSession().persist(masterList);
         _masterListManager.save(masterList, false);


Commented out stuff is stuff I've tried that didn't work either, do you know of any documentation on this anywhere??


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.