-->
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.  [ 7 posts ] 
Author Message
 Post subject: composite-id generation
PostPosted: Tue Sep 02, 2003 1:15 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
What is considered the best way to generate part of a composite key in a parent/child relationship?
tariff is the parent and has an id column, tariff_accessorial is the child and has a composite key of seq and tariff_id. Ideally, I'd like to be able to specify the sequence name for the child seq part of the key.
I read this in the wiki, but I don't think that's exactly what I'm looking for.

Current mappings (using http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd):

Parent table:
Code:
<hibernate-mapping>
  <class
      name="com.*****.cc.objectlib.tariff.TariffState"
      schema="eousr"
      table="TARIFF">
    <id name="id" type="java.lang.Long">
      <column name="TARIFF_ID"/>
      <generator class="sequence">
        <param name="sequence">tariff_seq</param>
      </generator>
    </id>
<!-- properties deleted for brevity -->
    <!-- associations -->
    <!-- bi-directional one-to-many association to TariffAccessorialState -->
    <set
        name="tariffAccessorialStates"
        lazy="true"
        inverse="true">
      <key>
        <column name="TARIFF_ID"/>
      </key>
      <one-to-many
          class="com.*****.cc.objectlib.tariff.TariffAccessorialState"/>
    </set>
<!-- other associations deleted for brevity -->
  </class>
</hibernate-mapping>


Child table:
Code:
<hibernate-mapping>
  <class
      name="com.*****.cc.objectlib.tariff.TariffAccessorialState"
      schema="eousr"
      table="TARIFF_ACCESSORIAL">
    <composite-id name="id" class="com.*****.cc.objectlib.tariff.TariffAccessorialIdentity">
      <key-property name="seqId" column="SEQ_ID" type="java.lang.Long"/>
      <key-property name="tariffId" column="TARIFF_ID" type="java.lang.Long"/>
    </composite-id>
<!-- properties deleted for brevity -->
<!-- associations -->
    <!-- bi-directional many-to-one association to TariffState -->
    <many-to-one
        name="tariffState"
        class="com.*****.cc.objectlib.tariff.TariffState"
        not-null="true"
        update="false"
        insert="false">
      <column name="TARIFF_ID"/>
    </many-to-one>
  </class>
</hibernate-mapping>


Is something like
Code:
    <composite-id name="id" class="com.*****.cc.objectlib.tariff.TariffAccessorialIdentity">
      <key-property name="seqId" column="SEQ_ID" type="java.lang.Long">
        <generator class="sequence">
          <param name="sequence">tariff_accessorial_seq</param>
        </generator>
      </key-property>
      <key-property name="tariffId" column="TARIFF_ID" type="java.lang.Long">
        <generator class="parent"/>
      </key-property>
    </composite-id>
or
Code:
    <composite-id name="id" class="com.*****.cc.objectlib.tariff.TariffAccessorialIdentity">
      <key-property name="seqId" column="SEQ_ID" type="java.lang.Long"/>
      <key-property name="tariffId" column="TARIFF_ID" type="java.lang.Long"/>
      <generator class="com.*****.cc.objectlib.tariff.IdentityGenerator">
        <param name="child_sequence">tariff_accessorial_seq</param>
      </generator>
    </composite-id>
possible?
Is the other option to implement IdentifierGenerator or PersistentIdentifierGenerator and handle the key generation myself?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 6:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you want to use a "generated" composite key (which is a seriously questionable notion), using a "custom identifier type", as per the wiki.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 8:25 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
gavin wrote:
If you want to use a "generated" composite key (which is a seriously questionable notion), using a "custom identifier type", as per the wiki.


Why is that a seriously questionable notion? Seems pretty typical to me. First part of key comes from parent sequence, second from a sequence unique to the child table.

insert into child values (parent_seq.currVal, child_seq.nextVal, column1, column2, ... )

We already have parent_seq.currVal in the parent object, all we need to "generate" the child composite key is the value from the child's sequence.

It has to be generated one way or another, this it is a known formula, why have the application do it when the framework can hide that? What is so different about a compound key that makes doing this in the framework a "questionable notion" while a simple key is a good idea?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 8:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If it is a generated, ie. "meaningless", value, why have two of them?


Whay not just have one, it will work just as effectively.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 9:07 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
How do you associate the tables then?

select C.*
from child C, parent P
where C.parent_id = P.id


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 1:11 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
After looking at the reference doc and examples again I think I was just confused. The parent part of the compound key doesn't have to be specified in the <composite-id> or as a <property>. Adding to my confusion was the fact that these mappings w/ the composite-ids were generated by the Hibernate plugin for Middlegen.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 2:20 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Methinks I'm doing this very thing, efender. http://forum.hibernate.org/viewtopic.php?t=61

I've written the code to do this but haven't been able to test it yet due to unrelated problems. Shall I post what I have?


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

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.