Hibernate version: 3.2.0
DB: Oracle 9
I have a table with a surrogate key as well as a natural key. The natural key has a uniqieness constraint. I find that when using hibernate I often get a unique constraint error.
My question: is there some way to make hibernate check whether the unique key exists so that rather than doing an insert when I saveOrUpdate it will do a select and use the current surrogate key if a record exists with the unique key?
I have tried all combinations of <natural-id> as well as the unique-key attribute to no avail.
If there is not an automatic way to handle this, what is the best way?
Here is a snippet of mapping document to show what I mean:
Code:
<class name="myclass" table="atable">
<id name="grpId" type="java.lang.Long">
<column name="GRP_ID" precision="10" scale="0" />
<generator class="sequence">
<param name="sequence">SEQ_GRP</param>
</generator>
</id>
<natural-id mutable="false">
<property name="grpNumber" type="string" unique-key="xxx">
<column name="GRP_NUMBER" length="16" not-null="true" />
</property>
<property name="sCode" type="string">
<column name="S_CODE" length="4" not-null="true" unique-key="xxx"/>
</property>
<property name="jCode" type="string">
<column name="J_CODE" length="4" not-null="true" unique-key="xxx"/>
</property>
</natural-id>
...