Hi,
I'm working with a legacy database in which there are no version fields. In addition, some of the tables have natural keys. This causes a problem because I cannot use the key to tell Hibernate when this is a new object and I cannot tell Hibernate to use a version.
When the key is a single field I can solve this by having a private id property with private access and having another property which is seen by the client code. Then I have an IdentifierGenerator which gets the value from the property for the id. An example mapping is shown below.
Code:
<class name="Country" table="COUNTRY">
<id name="id" column="CODE" type="string">
<generator class="CopyIdValueGenerator">
<param name="from_property">code</param>
</generator>
</id>
<property name="code" update="false" insert="false"/>
<property name="description" column="DESCRIPTION" type="string"/>
</class>
This works for a single key field but I also have a case with a natural composite key. I thought about doing something simplilar but I can't find a way to provide a generator for a composite key. Here's an example of a mapping which would ideally have a generator for the 'key' property.
Code:
<class name="Account" table="ACCOUNT">
<composite-id name="key" class="AccountIdentity">
<key-property name="id" column="ID"/>
<key-property name="code" column="CODE"/>
</composite-id>
<component name="identity" class="AccountIdentity">
<property name="id" insert="false" update="false"/>
<property name="code" insert="false" update="false"/>
</component>
<many-to-one name="country" column="country_code" class="Country"/>
<component name="currency" class="Currency">
<property name="code" column="CURRENCY" type="string"/>
</component>
</class>
So I guess I have 2 questions...
1) Is there a better way to achieve the required behaviour for a non-composite natural key when there is no version field?
2) How can I use Hibernate when I have a composite natural key and no version field?
Hibernate version: 2.1 but I'm happy to move to 3.0