I need some help getting a bidirectional list to work, I probably made a trivial mistake somewhere but I can't figure it out.
The code below works fine when using <set> in the mappings and Set and HashSet instead of List in the java code, however I need to have the children in their order which is not supported by <set>. So I tried to use the <list> tag which is now supported for bidirectional lists in Hibernate3.
The child table contains the "pos" column but all rows contain 0. When reading in, only one child is retrieved, not both.
Please advise what I have to do so that the pos column contains the index of my list.
I have omitted all constructors, getters and setters as they were generated by Eclipse.
Hibernate version: version 3.0 (28.2.2005)
Parent.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="sample">
<class name="Parent" table="parent">
<id name="id" type="long">
<generator class="increment"/>
</id>
<property name="name" type="string"/>
<list name="children" inverse="true" cascade="all-delete-orphan">
<key column="parent_id"/>
<list-index column="pos"/>
<one-to-many class="Child"/>
</list>
</class>
</hibernate-mapping>
Child.hbm.xml:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="sample">
<class name="Child" table="child">
<id name="id" type="long">
<generator class="increment"/>
</id>
<property name="name" type="string"/>
<property name="pos" type="integer"/>
<many-to-one name="parent" column="parent_id" not-null="true"/>
</class>
</hibernate-mapping>
Class Parent.java:Code:
package sample;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
public class Parent {
private Long id;
private String name;
private List children = new ArrayList();
// constructors, getters, setters generated by Eclipse,
// omitted here
public void addChild(Child child) {
children.add(child);
child.setParent(this);
}
public void removeChild(Child child) {
if (children.remove(child))
child.setParent(null);
}
}
Class Child.java:Code:
package sample;
public class Child {
private Long id;
private int pos;
private String name;
private Parent parent;
// constructors, getters, setters generated by Eclipse,
// omitted here
}
Main:Code:
package sample;
import java.util.Iterator;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
public class TestList {
public static void main(String[] args) {
Parent parent = new Parent();
parent.setName("Parent");
Child child1 = new Child();
child1.setName("Child 1");
parent.addChild(child1);
Child child2 = new Child();
child2.setName("Child 2");
parent.addChild(child2);
for (Iterator iter = parent.getChildren().iterator(); iter.hasNext();) {
Child child = (Child) iter.next();
System.out.println(child.getName());
}
try {
Configuration cfg = new Configuration();
cfg.addClass(Parent.class);
cfg.addClass(Child.class);
cfg.setProperty(Environment.HBM2DDL_AUTO, "create");
cfg.setProperty("hibernate.show_sql", "true");
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
s.save(parent);
tx.commit();
s.flush();
s.close();
s = null;
sf.close();
sf = null;
cfg = null;
} catch (Exception e) {
System.out.println("Exception in Hibernate:" + e.getMessage());
}
try {
Configuration cfg = new Configuration();
cfg.addClass(Parent.class);
cfg.addClass(Child.class);
cfg.setProperty("hibernate.show_sql", "true");
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Parent newParent = (Parent) s.load(Parent.class, new Long(1));
for (Iterator iter = newParent.getChildren().iterator(); iter.hasNext();) {
Child child = (Child) iter.next();
System.out.println(child.getName());
}
tx.commit();
s.flush();
s.close();
s = null;
sf.close();
sf = null;
cfg = null;
} catch (Exception e) {
System.out.println("Exception in Hibernate:" + e.getMessage());
}
}
}
Name and version of the database you are using: MySQL 4.0.20