-->
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: Relationship advice
PostPosted: Thu Feb 26, 2009 7:42 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
When I do this:

Code:
...

Set<Child> mySet = new HashSet<Child>();
mySet.add(lots of stuff);
parent.setChildSet(mySet);
aSession.save(parent);



I get the error org.hibernate.TransientObjectException:.

I want to save Parent AND the child set when I call .save();, and set parent_fk in each child to equals the parent PK.


Code:
public class Parent
{
  private  Set<Child> childSet = new HashSeT<Child>();

  public getChildSet()
  {
    return childSet; 
  }

}


Code:
public class Child
{
  private Long parent_fk;
  //Knows snothing of parent.
}


Code:
Parent Mapping

  <set name="childSet">
     <key column="parent_FK" />
     <one-to-many class="Parent" />
  </set>

Code:
Child Mapping

    <property name="parent_FK"  not-null="true" insert="true" type="java.lang.Long">
      <column name="PARENT_FK"/>
    </property>



Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 10:47 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 11:29 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Why do you need private Long parent_fk; in your child? If you really need it you have to set it as insert=false and update=false in your childmapping as this column is updated by the parent-child association.

You also need to add cascade to your one-to-many, so that childs get saved with the parent.

_________________
-----------------
Need advanced help? http://www.viada.eu


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.