Hi Guys
I am attempting to map a many-to-many relationship so that a Customer can have many "Properties" and each property is of a specific PropertyType.
The mapping looks like:
Code:
<class name="Customer" table="customer">
<id name="id">
<generator class="sequence">
<param name="sequence">CUSTOMER_ID_SEQ</param>
</generator>
</id>
<bag name="properties" lazy="false" cascade="save-update" table="customer_property">
<key column="customer_id"/>
<one-to-many class="CustomerProperty"/>
</bag>
</class>
<class name="PropertyType" table="property_type" mutable="false">
<id name="id">
<generator class="assigned"/>
</id>
<property name="name" column="name"/>
</class>
<class name="CustomerProperty" table="customer_property">
<id name="id">
<generator class="sequence">
<param name="sequence">CP_ID_SEQ</param>
</generator>
</id>
<property name="value" column="value"/>
<many-to-one name="propertyType" column="property_type_id" class="PropertyType" lazy="false" fetch="join"/>
</class>
This mapping works fine and allows me to fetch and update a Customer and their properties but my issue arises when I want to add a Property to a Customer.
I need a method on my Customer entity like addProperty( final String propertyName, final String value) (i.e. I know the name of the property type but I don't have the PropertyType object or it's primary key).
I think I need to define a UserType for the PropertyType column but I can't see any way that I can get a Session in a UserType so that I can load the appropriate PropertyType from the database.
Can anyone steer me in the best direction ?
Thanks
Andy