These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]

Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Composite Id With No Version Field
PostPosted: Mon Mar 28, 2005 12:18 pm 

Joined: Tue Mar 22, 2005 3:18 pm
Posts: 4
Location: Pennsylvania, USA

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.

    <class name="Country" table="COUNTRY">
        <id name="id" column="CODE" type="string">
            <generator class="CopyIdValueGenerator">
                <param name="from_property">code</param>
        <property name="code" update="false" insert="false"/>
        <property name="description" column="DESCRIPTION" type="string"/>

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.

   <class name="Account" table="ACCOUNT">
        <composite-id name="key" class="AccountIdentity">
            <key-property name="id" column="ID"/>
            <key-property name="code" column="CODE"/>
        <component name="identity" class="AccountIdentity">
            <property name="id" insert="false" update="false"/>
            <property name="code" insert="false" update="false"/>
        <many-to-one name="country" column="country_code" class="Country"/>
        <component name="currency" class="Currency">
            <property name="code" column="CURRENCY" type="string"/>

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

Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.