Hi!
I'm a hibernate newbie and I'm getting some strange results with a one-to-many relation, relating an object to childobjects of the same class. My one-to-many Set seems to contain only 1 object, even though I know for a fact it's supposed to contain several. If I change the Set into a List, I get all objects but the list also contains several "Null"-values.
Code:
My table:
-----------
id name skill_id
1 node with 3 childnodes <NULL>
2 a node 1
3 node with 1 childnode 1
4 another node 1
5 last node 3
Code:
My mapping :
-----------------
<hibernate-mapping package="modules.skills">
<class name="Skill" table="Skill" lazy="true">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<set name="subskills" lazy="true" inverse="true">
<key column="Skill_id"/>
<one-to-many class="modules.skills.Skill"/>
</set>
<many-to-one name="parent" column="Skill_id" class="modules.skills.Skill"/>
</class>
</hibernate-mapping>
Code:
My class:
------------
public class Skill {
private int id;
private String name;
private Integer skill_id;
private Set subskills;
private Skill parent;
public Skill() {}
setters&getters
Code:
My code :
------------
Iterator it = con.getClientHibernateSession().createQuery( "from modules.skills.Skill as Skill").list().iterator();
while( it.hasNext() ) {
Skill skill = (Skill)it.next();
System.out.println("name :" + skill.getName() );
System.out.println("children :" + skill.getSubskills().size());
Set l = skill.getSubskills();
for (Object o : l) {
Skill t = (Skill)o;
if(t!=null) {
System.out.println( "-->" + t.getName());
}
else {
System.out.println("-->null");
}
}
if( skill.getParent()!=null ) {
System.out.println("parent : " + skill.getParent().getName());
}
System.out.println("");
}
Code:
My output:
-------------
name :node with 3 childnodes
children :1
-->a node
name :a node
children :0
parent : node with 3 childnodes
name :node with 1 childnode
children :1
-->last node
parent : node with 3 childnodes
name :another node
children :0
parent : node with 3 childnodes
name :last node
children :0
parent : node with 1 childnode
Any ideas?