-->
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.  [ 10 posts ] 
Author Message
 Post subject: Relationship table with attributes
PostPosted: Thu Dec 04, 2003 10:23 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
I have a requirement to support attributes in an Association table being used in many-to-many association. For example, consider the many-to-many relationship from Group and User classes, Group can contain multiple sub Groups as well as multiple Users. Also, Users and Groups can be children of multiple Parent Groups.

I have a User class(in User table), Group class(in Group table) and an association table group_membership (with no association class). The group_membership table can contain the following columns:

parent_group_id NOT NULL(foreign key to Group table)
child_group_id NULL (foreign key to Group table)
child_user_id NULL (foreign key to User table)
permissions (additional relationship attribute required)

How do I specify the mapping in Group class to get/set the childGroups (including the relationship attribute permissions)?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 7:10 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Have a look at section 7.2 of the reference guide, the second example.
And you can use the search tool of the forum. I remember answerig that question... yesterday ;-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Relationship table with attributes
PostPosted: Mon Dec 08, 2003 7:54 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
Thanks for reply.

I have one more question on the same topic.
If my association table group_membership has the following attributes :

parent_group_id NOT NULL(foreign key to Group table)
child_id NULL NOT NULL (Not a foreign key but the id will be either pointing to Group table or User table)
child_type NOT NULL ('U' - means child is a User, 'G' - means child is a Group)
permissions (additional relationship attribute required )

How do I specify the many-to-many relationship from Group to User using the above mentioned group_membership table structure?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 7:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use <any> inside the <composite-element> mapping.


Top
 Profile  
 
 Post subject: Use <any> inside the <composite-element> mapping
PostPosted: Wed Dec 24, 2003 1:40 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
Gavin,

<composite-element> does not accept <any> in it. I get the following error if I add any inside <composite-element>:

ERROR [Thread-4] 2003-12-23 23:42:18,735 - net.sf.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(180) The content of element type "composite-element" must match "(parent?,(property|many-to-one|nested-composite-element)*)".

Even I verified the dtd, it doesn't accept it. Here is my mapping:

<set name="Persons" table="groupmembership" lazy="true" inverse="true">
<key column="group_id"/>
<composite-element class="net.sf.hibernate.examples.quickstart.GroupMembership">
<any name="members" id-type="string" meta-type="net.sf.hibernate.examples.quickstart.GroupTypeMapper">
<column name="child_type"/>
<column name="child_id"/>
</any>
</composite-element>
</set>


Any thoughts?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2003 6:09 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Try to update the DTD in your jar to support it. From a very quick look, it *might* work (at least it will bind properly).

Submit a bug to Jira in order to follow this topic.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: <any> inside the <composite-element> mapping
PostPosted: Tue Jan 06, 2004 3:38 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
Updating the DTD worked. I looked at the SQLs that were executed, and I noticed that there was one SQL to retrieve all rows from the relationship table for a group id (I specified in my code) and for each record in the relationship table based on the child type (User or Group), one select SQL issued to retrieve either the from the User table or from the Group table.

Is there anyway, hibernate would be able to retrieve all the members of the group(users and child groups) in one SQL by joining the relationship table, user and group tables using the <composite-element> and <any> constructs?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 3:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't know about the any, but try with lazy="false"

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 10:33 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
It doesn't help with lazy="false".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 3:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Is there anyway, hibernate would be able to retrieve all the members of the group(users and child groups) in one SQL by joining the relationship table, user and group tables using the <composite-element> and <any> constructs?


Nope. <any> does not support outer join loading (this is not unreasonable).

Better to make the associated class with lazy="true".


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