I'm trying to make a tree structure using HB, But a got a weird problem I don't know how to solve:
I insert some nodes to the tree, everything go ok.
When I try to get them back(createCriteria and/or createQuery), the getChildren List is ALWAYS null. getParent works fine.
Can you guys help me? Thank you a lot.
All files:
Hibernate version:305
Mapping:
Code:
<hibernate-mapping>
<class name="br.com.meta.satie.hb.sistema.Node" lazy="true">
<id name="id">
<generator class="increment"/>
</id>
<property name="title"/>
<property name="path"/>
<property name="iconPath"/>
<property name="sequence"/>
<list name="children" table="node"
cascade="all-delete-orphan" inverse="true" lazy="true">
<key column="idParent" />
<index column="sequence" />
<one-to-many class="br.com.meta.satie.hb.sistema.Node" />
</list>
<many-to-one
name="parent"
class="br.com.meta.satie.hb.sistema.Node"
column="idParent"
cascade="all"/>
</class>
</hibernate-mapping>
INSERTCode:
Transaction tx = session.beginTransaction();
Node ROOT = new Node();
ROOT.setTitle("ROOT");
ROOT.setParent(null);
ROOT.setSequence(0);
ROOT.setPath("ROOT path");
Node CHILD1 = new Node();
CHILD1.setTitle("CHILD1");
CHILD1.setParent(ROOT);
CHILD1.setSequence(0);
CHILD1.setPath("CHILD1 path");
Node CHILD2 = new Node();
CHILD2.setTitle("CHILD2");
CHILD2.setParent(ROOT);
CHILD2.setSequence(0);
CHILD2.setPath("CHILD2 path");
session.save(ROOT);
session.save(CHILD1);
session.save(CHILD2);
tx.commit();
QUERY:Code:
Query select = session.createQuery("from Node n left join fetch n.children");
List<Node> nodes = select.list();
for(Node n: nodes) {
System.out.println("NODE: " + n.getId() + " PARENT: " + n.getParent() + " CHILDREN: " + n.getChildren());
}
Name and version of the database you are using:MySQL
The generated SQL (show_sql=true):Code:
Hibernate: /* insert br.com.meta.satie.hb.sistema.Node */ insert into Node (title, path, iconPath, sequence, idParent, id) values (?, ?, ?, ?, ?, ?)
Hibernate: /* insert br.com.meta.satie.hb.sistema.Node */ insert into Node (title, path, iconPath, sequence, idParent, id) values (?, ?, ?, ?, ?, ?)
Hibernate: /* insert br.com.meta.satie.hb.sistema.Node */ insert into Node (title, path, iconPath, sequence, idParent, id) values (?, ?, ?, ?, ?, ?)
Hibernate: /* from Node p left join fetch p.children c */ select node0_.id as id0_, children1_.id as id1_, node0_.title as title0_0_, node0_.path as path0_0_, node0_.iconPath as iconPath0_0_, node0_.sequence as sequence0_0_, node0_.idParent as idParent0_0_, children1_.title as title0_1_, children1_.path as path0_1_, children1_.iconPath as iconPath0_1_, children1_.sequence as sequence0_1_, children1_.idParent as idParent0_1_, children1_.idParent as idParent0__, children1_.id as id0__, children1_.sequence as sequence0__ from Node node0_ left outer join Node children1_ on node0_.id=children1_.idParent
SYSTEM OUTPUT:
NODE: 1 PARENT: br.com.meta.satie.hb.sistema.Node@8e32e7 CHILDREN: null
NODE: 2 PARENT: null CHILDREN: null
NODE: 2 PARENT: null CHILDREN: null
NODE: 3 PARENT: br.com.meta.satie.hb.sistema.Node@8e32e7 CHILDREN: null
BUILD SUCCESSFUL (total time: 5 seconds)