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`)
) ;