-->
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.  [ 6 posts ] 
Author Message
 Post subject: help! problem with one to mamy
PostPosted: Wed Mar 03, 2004 10:49 am 
Newbie

Joined: Wed Mar 03, 2004 10:26 am
Posts: 2
When I using hibernate update talbe,the parent table can be updated succesful,but the child table only do insert,can't update the old records

Parent.hbm.xml
Code:
<hibernate-mapping>
<class name="com.websh.hibernate.model.Parent" table="parent" dynamic-update="true" dynamic-insert="true">
<id column="parentid" name="parentid" type="java.lang.Long">
<generator class="increment"/>
</id>
<property column="parentname" length="50" name="parentname" type="java.lang.String"/>

        <bag name="childs" cascade="all" inverse="true" >
            <key column="parentid"/>
            <one-to-many class="com.websh.hibernate.model.Child"/>
        </bag>

</class>
</hibernate-mapping>


Child.hbm.xml
Code:
<hibernate-mapping>
<class name="com.websh.hibernate.model.Child" table="child" dynamic-update="true" dynamic-insert="true">
<id column="childid" name="childid" type="java.lang.Long">
<generator class="increment"/>
</id>


<property column="childname" length="50" name="childname" type="java.lang.String"/>

<many-to-one name="parent" class="com.websh.hibernate.model.Parent" not-null="true" column="parentid"/>

</class>
</hibernate-mapping>



Child.java
Code:
  public Parent getParent() {
    return parent;
  }

  public void setParent(Parent parent) {
    this.parent = parent;
  }


Parent.java
Code:
    public Collection getChilds() {
      if (childs==null){
        childs = new ArrayList();
      }
        return this.childs;
    }

    public void setChilds(Collection childs) {
        for (Iterator iter = childs.iterator(); iter.hasNext(); ) {
          Child item = (Child)iter.next();
          item.setParent(this);
        }
        this.childs = childs;
    }



method

Code:
  public static void update(){
    Collection col = new ArrayList();
    try{
      Configuration cfg = new Configuration().configure();
      SessionFactory sessions = cfg.buildSessionFactory();
      Session session = sessions.openSession();
      Transaction tx = session.beginTransaction();

      Child c = new Child();
      for (int i = 0; i < 3; i++) {
        c.setChildname("儿子up"+i);
        col.add(c);
      }
      Parent p = new Parent();
       p = (Parent)session.load(Parent.class, new Long(1));

      p.setChilds(col);
      p.setParentname("a");

      session.update(p);
      session.flush();
      tx.commit();
      session.close();
    }catch(Exception e){
      e.printStackTrace();
    }
  }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 10:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well I don't see you doing an update of a child in your code anywhere ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 12:05 am 
Newbie

Joined: Wed Mar 03, 2004 10:26 am
Posts: 2
Shoule I update the childs manual?
I think if I use one to many ,when I update the parents, the childs will be updated automatic.

thank for your reply.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 12:49 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
suggestion- don't "set" the children,- get a handle to the collection, then add/remove/update using the existing collection. When you load your parent object, just call
parent.getChildren().add (for an insert)
or for an update
iterator it = parent.getChildren().
while (it.next())
{
//find the object needing update
//and modify existing object in this collection
Child child = (Child) it.next();
if (child.getId().equals("one needing update")
child.setXXX()



}
instead of
parent.setChildren(some new collection)



I'm not sure this is the problem, but the example in the docs explains it in a way that suggests adding/removing from the existing collection, not replacing with a new collection.

check out 9.2 in the manual...

James


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 8:25 am 
Newbie

Joined: Wed Jan 14, 2004 3:56 pm
Posts: 7
Location: Ann Arbor, MI, USA
Given your update method, it acts exactly as it should. You create a collection three new children, set their names, but their childid's are left null. You load the parent and set the new collection for the children, then you update the parent. Hibernate sees that the parent has three new children (ids are null) and inserts them.

If you want to update existing children then load the parent, loop through the children and change them, then update the parent.

Will


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 5:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In your sample you don't update your child objects, you insert them (they"re new)

_________________
Emmanuel


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