-->
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.  [ 4 posts ] 
Author Message
 Post subject: Parent - children association
PostPosted: Wed Feb 23, 2005 5:12 pm 
Newbie

Joined: Tue Feb 22, 2005 1:18 pm
Posts: 3
Hi

Well, I’ve problem with simple parent-children association (Hibernate 2.1.8). I’ve read manual and I really don’t see where could be an error.

…begin trans
parent = hs.load(some.namespace.Parent, id);
parent.addChild(child);
…end trans and session close

This causes net.sf.hibernate.HibernateException: Batch update row count wrong: 0

…And this works:
…begin trans
parent = hs.load(some.namespace.Parent, id);
parent.addChild(child);
session.save(child);
…end trans and session close

Another problem is with loading:

parent = session.get(some.namespace.Parent, id); // fetched correctly
set = p.getChildren();


…And set.size() == 0 !!! but child rows exists in database. I don’t understand, ‘cause cascade="all” is there …
Code:
====== Child.java ========
package some.namespace;

public class Child {
 
  private int ID;
  private Parent parent;

  public Child() {}
 
  public Parent getParent() {
    return parent;
  }
  public void setParent(Parent parent) {
    this.parent = parent;
  }
  public int getID() {
    return ID;
  }
  public void setID(int id) {
    ID = id;
  }
}
========= Parent.java =======
package some.namespace;

import java.util.HashSet;
import java.util.Set;


public class Parent {
 
  private int ID;
 
  private Set children = new HashSet();
 
  public Parent() {}
 
  public Set getChildren() {
    return children;
  }
  public void setChildren(Set children) {
    this.children = children;
  }
  public int getID() {
    return ID;
  }
  public void setID(int id) {
    ID = id;
  }
 
  public void addChild(Child w) {
    w.setParent(this);
    children.add(w);
  }
}

<hibernate-mapping package=" some.namespace">
   <class name="Child" table="children">
      <id name="ID" column="id" type="int">
         <generator class="native"/>
      </id>
      <many-to-one name="parent" column="id_parent"
         class="Parent" not-null="true"/>
      
   </class>
</hibernate-mapping>

<hibernate-mapping package=" some.namespace">
   <class name="Parent" table="parents">
      <id name="ID" column="id" type="int">
         <generator class="native"/>
      </id>

      <set name="children" inverse="true" cascade="all-delete-orphan">
         <key column="id"/>
         <one-to-many class="Child"/>
      </set>
   </class>
</hibernate-mapping>

====== MySQL  tables ====
CREATE TABLE `parents` (
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
);

CREATE TABLE `children` (
  `id` int(11) NOT NULL auto_increment,
  `id_parent` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 7:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
check your unsaved-value mapping


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 10:31 am 
Newbie

Joined: Tue Feb 22, 2005 1:18 pm
Posts: 3
I've aded unsaved-value="0" and private int ID=0 in both classes and mappings. First problem solved.

But collection now contains only one element but for sure there's many of those. What can be missing ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 10:39 am 
Newbie

Joined: Tue Feb 22, 2005 1:18 pm
Posts: 3
strange, 'couse i dont use lazy loading.


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