-->
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.  [ 5 posts ] 
Author Message
 Post subject: Copy child collections to new parent
PostPosted: Fri Feb 16, 2007 1:00 am 
Newbie

Joined: Thu Feb 15, 2007 12:48 pm
Posts: 11
Hi,
I am new to Hibernate and have been looking for almost two days for a solution and still had no luck.
Can someone give me a small example as how to copy all the child elements from parent1 to parent2. Note: Parent2 is transient, not yet updated to the database, children are of type bag.

Code:
Parent parentOne = Session.Load(typeof(Parent), 1);
Children childrenOne = parentOne.Children;

Parent parentTwo = new Parent();
foreach (Child child in childrenOne)
{
    parentTwo.Children.Add(new Child(child.Value));
}
Session.Save(parentTwo)



Can someone tell me what I am doing wrong? I get a NonUniqueException when I try to Save.

Thanks for any help,
Javid

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 3:44 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post your mapping files ? Looks like a problem with your collection mapping. Hay your tried a many-to-many mapping ?

Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 10:59 am 
Newbie

Joined: Thu Feb 15, 2007 12:48 pm
Posts: 11
Thanks for your response. Here are my mapping files:

Parent : Template.hbm.xml
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="HaynesPrototype" assembly="HaynesPrototype">
<class name="HaynesPrototype.Template" table="SCENARIO_TEMPLATES">
<id name="ID" column="ID" unsaved-value="0" type="integer" length="50">
<generator class="identity" />
</id>
<property name="TemplateName" column="TEMPLATE_NAME" type="string" length="40" />
<bag name="TemplateValues" table="SCENARIO_TEMPLATE_VALUES" access="property"
inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="TEMPLATE_ID" />
<one-to-many class="TemplateValue" />
</bag>

</class>

Child: TemplateValue.hbm.xml
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="HaynesPrototype" assembly="HaynesPrototype">
<class name="HaynesPrototype.TemplateValue" table="SCENARIO_TEMPLATE_VALUES">
<composite-id>
<key-many-to-one name="Parent" class="Template">
<column name="TEMPLATE_ID" />
</key-many-to-one>
<key-property name="Type" column="TYPE" type="string" />
<key-property name="Value" column="VALUE" type="string" />
</composite-id>
<many-to-one name="Parent" class="Template" column="TEMPLATE_ID" insert="false" update="false" not-null="true" />
</class>


</hibernate-mapping>

Thanks for your help

Javid


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 9:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Hi Javid,

I've misunderstood your question in the first place. Thought you want to attach the same child to another parent. Considering your second post to this problem, you really want to make a deep copy.
I haven't used composite ids yet, so I can only guess. As far as I understood composite ids from studying the reference, you have to implemented Equals and GetHashCode in TemplateValue. It also needs to be Serializable [from reference 5.1.5]. If not, Hibernate could probably not decide, if you new instance is unsaved. Have a look at the reference chapter 7.4, too.

Why don't you use a generated id on TemplateValue instead of a composite id that contains all columns of the table. You can use the generated id as primary key and define a unique constraint on (parentid, type,value).

Hope that helps,
Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 2:27 pm 
Newbie

Joined: Thu Feb 15, 2007 12:48 pm
Posts: 11
Hi wolfgang,
Thanks for your response. I found the problem, my GethashCode() was not implemented right, so it's working fine now.

Thanks again,
Javid


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