I'm trying to determine how or if I can map two different collections to the same table. I have a TaskForceAgenda object which has two collection properties: personnelAssignments and resourceAssignments. Both collections contain the same type of member class: TaskForceAssignment.
The TaskForceAssignment has two associations: one to a TaskForceMember and one to a TaskForceResource. In the personnelAssignments collection, the TaskForceResource property of TaskForceAssignment is null. In the resourceAssignments collection, the TaskForceMember property of TaskForceAssignment is null. I'd like to map both collections to the same table in such a way that when I load the TaskForceAgenda object, the two collections are mutually exclusive. Does Hibernate support this type of mapping or must I map the two collecitons to separate tables? My environment is hibernate 3.1 and SQL Server.
My TaskForceAgenda mapping file is included. It currently maps the two collections to separate tables. I'd like to modify it so that both collections are mapped to the same table and when loaded neither collection contains elements from the other collection.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="gov.seahawk.model">
<!-- Hibernate class mapping -->
<class name="TaskForceAgenda" table="TASK_FORCE_DAILY_AGENDA" dynamic-insert="true" dynamic-update="true">
<id name="key" column="ID" type="string">
<generator class="guid"/>
</id>
<property name="date" type="date" column="AGENDA_DATE"/>
<set name="personnelAssignments" table="DAILY_PERSONNEL_ASSIGNMENTS" lazy="false" cascade="all">
<key column="AGENDA_ID"/>
<many-to-many class="TaskForceAssignment" column="PERSONNEL_ASSIGNMENT_ID" />
</set>
<set name="personnelStatuses" table="DAILY_PERSONNEL_STATUSES" lazy="false" cascade="all">
<key column="AGENDA_ID" />
<many-to-many class="TaskForceMember" column="PERSONNEL_STATUS_ID" >
</many-to-many>
</set>
<set name="resourceAssignments" table="DAILY_RESOURCE_ASSIGNMENTS" lazy="false" cascade="all">
<key column="AGENDA_ID" foreign-key="DAILY_RESOURCE_ASSIGNMENTS_AGENDA_ID_FK"/>
<many-to-many class="TaskForceAssignment" column="RESOURCE_ASSIGNMENT_ID" />
</set>
<set name="resourceStatuses" table="DAILY_RESOURCE_STATUSES" lazy="false" cascade="all">
<key column="AGENDA_ID" />
<many-to-many class="TaskForceResource" column="RESOURCE_STATUS_ID" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="gov.seahawk.model">
<class name="TaskForceAssignment" table="TASK_FORCE_ASSIGNMENT" dynamic-insert="true" dynamic-update="true">
<id name="key" column="ID" type="string">
<generator class="guid"/>
</id>
<property name="assignment" type="string" column="ASSIGNMENT" length="100"/>
<property name="taskForceUnit" type="string" column="TASK_FORCE_UNIT" length="10"/>
<property name="regularHours" type="float" column="REGULAR_HOURS"/>
<property name="overtimeHours" type="float" column="OVERTIME_HOURS"/>
<many-to-one
name="member"
class="TaskForceMember"
column="TASK_FORCE_MEMBER_ID"
cascade="none"
lazy="false" />
<many-to-one
name="resource"
class="TaskForceResource"
column="TASK_FORCE_RESOURCE_ID"
cascade="none"
lazy="false" />
</class>
</hibernate-mapping>