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