Hibernate version: 3.1
Hi @all,
i have a many-to-many association between my domain classes Employee and Organization.
In the first iteration of my application i had an additional property on the association table connecting both.
I mapped it using a composite-element, as you can see in the provided mapping for Employee at the bottom.
This approach worked out quite nicely but there is a demand for several changes in my domain model :-(
and now i need a collection property on the association table.
The connecting class (value type) OrganizationEmployeeComponent should now contain a set of Authority objects
instead of a simple boolean flag. This is needed because a certain Employee can hold multiple
Authority objects for a certain Organization. Authority itself is an entity. The relationship between
OrganizationEmployeeComponent and Authority is many-to-many but should be navigable
only from OrganziationEmployeeComponent to Authority and not vice versa.
I don't know how to map this collection property. From what i found in the
ref manual it's impossible to have a collection property within a composite-element. Do i need to use a connecting entity class between
Employee and Organization with one-to-many mappings on each side instead of a composite-element? I would be glad for any hints or code examples!
Many thanks in advance!
Mapping documents:
Organization.hbm.xml:
Code:
<hibernate-mapping package="de.tevege.bpr.domain">
<class name="Organization" table="ORGANIZATION">
<id name="id" column="id" type="long">
<generator class="sequence"/>
</id>
<property name="name" column="name" type="string"/>
<property name="street" column="street" type="string"/>
<property name="city" column="city" type="string"/>
<property name="postalcode" column="postalcode" type="int"/>
<property name="phone_nr" column="phone_nr" type="string"/>
<property name="fax_nr" column="fax_nr" type="string"/>
<property name="email" column="email" type="string"/>
<!--
Mapping for component association.
This is an alternative mapping for the many-to-many association
to Employee. We use an intermediate class, OrganizationEmployeeComponent, that
represents the link (However, it is a value type in this case)
We need to use this mapping style because our association table has
additional columns. In our case, we have the flag for authority.
The many-to-many association we created here is only accessible from the
Organziation side. Employees can't access OrganizationEmployeeComponent,
it is mapped as a non-shared component of Organziation.
-->
<set name="organzationEmployeeComponents"
table="ORGANIZATION_EMPLOYEE_COMPONENTS"
cascade="save-update">
<key>
<column name="ORGANIZATION_ID" not-null="true"/>
</key>
<composite-element class="OrganizationEmployeeComponent" >
<many-to-one name="employee"
column="EMPLOYEE_ID"
class="Employee"
not-null="true"
cascade="save-update"/>
<property name="isAuthority"
column="isAuthority"
type="boolean"
not-null="true"/>
</composite-element>
</set>
</class>
</hibernate-mapping>