I'm fairly new to Hibernate (using Hibernate 3) so for the past couple of days I've been struggling with the following:
I have a Campaign object which has a reference to a GeoTarget object that can be reused accross multiple campaigns. In a turn, GeoTarget consists of two lists one for the countries and another one for the regions. Each individual country or region is represented by Constant object that provides string value (e.g. USA) and description. Once country or region value is defined, I want to reuse it in the future mappings so the constant is mapped as many-to-many. The problem is that when I try to cascade on new or existing campaign object Hibernate will attempt to insert new constant value regardless if it's already exists in the table.
So, the question is - can cascading be done at all or should I remove cascade and do SELECT on each Constant object everytime I need to add another campaign?
The following are HBM snippets:
Campaign.hbm
Code:
<class name="CampaignBean" table="CAMPAIGNS">
... id + props
<many-to-one name="geoTargeting" column="GEO_TARGET_ID" class="GeoTargetBean"
lazy="proxy" cascade="merge"/>
</class>
GeoTarget.hbmCode:
<class name="GeoTargetBean" table="GEO_TARGETS">
... id + props
<list name="countries" table="GEO_COUNTRIES" lazy="true" cascade="merge">
<key column="PARENT_ID"/>
<list-index column="ID"/>
<many-to-many column="CONSTANT_ID" class="Constant"/>
</list>
<list name="regions" table="GEO_REGIONS" lazy="true" cascade="merge">
<key column="PARENT_ID"/>
<list-index column="ID"/>
<many-to-many column="CONSTANT_ID" class="GoogleConstant"/>
</list>
</class>
Constant.hbmCode:
<class name="Constant" table="CONSTANTS" select-before-update="true">
<id name="id" column="ID" type="long">
<generator class="native">
<param name="sequence">seq_google_const</param>
</generator>
</id>
<!-- When I set unique="false" everything works but I get bunch of duplicates in CONSTANTS table -->
<property name="value" type="string" not-null="true" column="VALUE" unique="true"/>
<property name="description" type="string" column="DESCRIPTION"/>
</class>
P.S. I tried all flavors of cascade including "all" and combinations such as "merge, save-update"