-->
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.  [ 4 posts ] 
Author Message
 Post subject: Help with many-to-one relationship using discriminator
PostPosted: Thu Mar 04, 2004 10:35 pm 
Newbie

Joined: Thu Mar 04, 2004 9:38 pm
Posts: 4
I am having problems creating a many-to-one relationship between two tables in a scenario with a discriminator column determining which foreign table to reference.

Consider the following schema:

Code:
Table: USERS
UserID int(4) PK
Login varchar(50)

Table: GROUPS
GroupID int(4) PK
GroupName varchar(50)

Table: PERMISSIONS
PermissionID int(4)
UserOrGroupID int(4) FK to User/Group
AssigneeType char(1) // 'U' for user or 'G' for group


So, the UserOrGroupID references either the USER or GROUP table depending on the status of the AssigneeType flag.

In the Permission java object I have a field named 'assignee' that I want to point to either a group or user. It is of type Assignee which is just an interface implemented by both User and Group objects.

I do not know how to create the hbm file for the Permission object specifically the assignee field. I really want something like the following:

If UserOrGroup = 'U' use this xml block:

<many-to-one name="assignee" class"eg.user"
column="UserOrGroupID" />

else if UserOrGroup = 'G' use this xml block:

<many-to-one name="assignee" class"eg.group"
column="UserOrGroupID" />


Any help would be greatly appreciated.

Thanks,
Thad

Hibernate: v2.1


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 11:12 pm 
Beginner
Beginner

Joined: Tue Jan 27, 2004 2:14 pm
Posts: 40
Location: Atlanta, GA, USA
I don't think you will be able to use the discriminator this way.

This table layout seems wierd to me. Maybe you could just have both the UserID and the GroupID in the Permissions and map both objects, but that would still be pretty awkward. Sorry I'm not much help for you. If you are not stuck to the table layout (legacy), you might want to reconsider this design.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 11:18 pm 
Newbie

Joined: Thu Mar 04, 2004 9:38 pm
Posts: 4
Thanks for the response. I considered doing exactly what you proposed regarding adding both the UserID and the GroupID, but not only is it awkward as you mentioned, but not scalable at all. Imagine if the discriminator could be 6 or 7 values instead of 2.

Unfortunately, I am locked into this schema although I cant really think of a better one. Merging the USER and GROUP tables seems equally clunky to me.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 11:29 pm 
Beginner
Beginner

Joined: Tue Jan 27, 2004 2:14 pm
Posts: 40
Location: Atlanta, GA, USA
Have you considered using the <join-subclass>? Maybe something like..

Code:
Table: ASSIGNEE
Table: USER
Table: GROUP

Class AssigneeImpl implements Assignee

Class User extends  AssigneeImpl

Class Group extends  AssigneeImpl

Class Permissions
private Assignee assignee; 


Then is the Permission object you would map to an Assignee. So you would always get back an AssigneeImpl, but if you did an instance of on it, you would get a User or a Group.

This would require an additional table (ASSIGNEE), which may be out of the question for you. Anyway, just an idea. Good Luck!!!!


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