-->
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.  [ 5 posts ] 
Author Message
 Post subject: Bidirectional Relation only working one way
PostPosted: Thu Dec 21, 2006 11:38 am 
Newbie

Joined: Thu Dec 21, 2006 11:22 am
Posts: 2
Hi there, I'm fairly new to hibernate but have so far managed quite well with mapping objects and pulling stuff from my mysql database.

I'm having trouble with a particular relationship in my model, I have a class called group and another called category, what I'm trying to achieve is one-to-many relationship so that one group can have many categories, the relation also needs to be bidirectional so I can pull the group from any given category.

Here is the relation in my mapping files, this one from category:

Code:
<many-to-one name="group" cascade="all" lazy="false" class="tracker.persistent.Group" not-null="true" foreign-key="fk_cat_group">
            <column name="group_fk" />
</many-to-one>


And this one from group:

Code:
<set name="categories" table="t_category" lazy="false" cascade="all" inverse="true" >
            <key column="group_fk"/>
            <one-to-many class="tracker.persistent.Category" />
</set>


The relation works from category to group however when I execute the relation the other way (i.e. group.getCategories) I end up with a null set which I know from executing standard sql against the database is not the case.

I'm using mysql 5 with hibernate 3

Any help would be gratefully received,
Many thanks,
Oli


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:25 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Are you sure the cascade="all" attribute on the <many-to-one> is wanted? It's saying to Hibernate that if you delete a category, it should cascade deletion to its parent ?!

Moreover, putting cascading on both side is quite unusual. IMO, you should remove this cascade="all", so as your mapping is something like :
Code:
<many-to-one name="group" lazy="false" not-null="true" foreign-key="fk_cat_group" column="group_fk" />

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 6:11 pm 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:13 pm
Posts: 32
Location: Spain (GU)
You had better change the "set tag" for something like this:

Code:
<set name=" categories" inverse="true">
   <key column="group_fk"/>
   <one-to-many class=" tracker.persistent.Category "/>
</set>


Best regards.

_________________
Please don't forget the credit system.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 12:11 pm 
Newbie

Joined: Thu Dec 21, 2006 11:22 am
Posts: 2
Hi there,

thanks for your help so far however I'm still having the same problem, whenever I do goup.getCategories I have a null object returned, should this really be the case or should I get back an empty set?

In any case I should be getting a populated set as doing a direct sql statement against the db returns 25 child categories for my given group.

Any thoughts and advice gratefully recieved,
Thanks again,
Oli


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 4:59 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Did you try the mapping Javier Martinez and I provided? You say that using SQL, you're sure that the given group has categories: did you do it using the query Hibernate generates? Is this generated query correct or not?

Using log4j: you should activate the equivalent of show_sql="true" in hibernate.cfg.xml => org.hibernate.SQL

=> add this to your log4j.properties file
Code:
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.bind=debug

This will let you see the exact query Hibernate sends and the value it uses for parameters.

If the query is correct, then I can't see why Hibernate wouldn't give you your 25 instances.

About null or empty set, I don't remember. I'd say null, but I also know that Hibernate uses something like ArrayList.EMPTY_SET sometimes. I'll try and check and let you know.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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