-->
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.  [ 2 posts ] 
Author Message
 Post subject: problem with one-to-many relation
PostPosted: Wed Dec 07, 2005 7:09 am 
Newbie

Joined: Wed Dec 07, 2005 6:31 am
Posts: 9
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?

_________________
OJ


Top
 Profile  
 
 Post subject: partly solved
PostPosted: Wed Dec 07, 2005 8:59 am 
Newbie

Joined: Wed Dec 07, 2005 6:31 am
Posts: 9
My skill class was extending some old abstract class that overrode equals(), hashCode() and compareTo(). These methods doesn't seem to work with hibernate, and my results became as expected when I removed the overrides.

I still get the NULL-values in my List, but I guess I can live with the Set for now.

Anyone had similar problems with the List?

OJ

_________________
OJ


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