-->
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.  [ 2 posts ] 
Author Message
 Post subject: The Data Architects hate me... Composite ID containing anoth
PostPosted: Fri Apr 03, 2009 3:49 pm 
Newbie

Joined: Thu Oct 09, 2008 12:51 pm
Posts: 7
For the record, this wasn't my idea. Any the DA's experience and comprehension is so far ahead of mine it is inconceivable I question their design or patterns.

I have three tables:

Customer, Client, Group

Customer has an integer key:cust_id
Client has a composite index of [FK to Customer, Integer key:client_id]
Group has a composite index of [FK to Customer, FK to Client, Integer Key:group_id]

Just trying to figure out the correct way to map this. Technically, the composite id for Group contains the composite index for client. Agh!

Here is what I have so far:

Customer
Code:
public class Customer implements java.io.Serializable {
   private Integer custId;
....


Map
Code:
<hibernate-mapping>
   <class name="hibernate.generated.Customer" table="CUST">
      <id name="custId" type="int">
         <column name="CUST_ID" />
         <generator class="assigned" />
      </id>
....


ClientId
Code:
public class ClientId implements java.io.Serializable {
   private Customer custId;
   private int clntId;
...


Map
Code:
<hibernate-mapping>
   <class name="Client" table="CLIENT" >
      <composite-id name="id" class="hibernate.generated.ClientId">
         <key-many-to-one class="Customer" name="custId">
            <column name="CUST_ID" />
         </key-many-to-one>
         <key-property name="clntId" type="int">
            <column name="CLNT_ID" />
         </key-property>
      </composite-id>
....


Group
Code:
public class GroupId implements java.io.Serializable {
   private Client clntId;
   private Integer grpId;
...


Map
Code:
<hibernate-mapping>
    <class name="hibernate.generated.Group" table="GRP" >
        <composite-id name="id" class="hibernate.generated.GroupId">
            <key-many-to-one name="clntId" class="hibernate.generated.Client">
                <column name="CLNT_ID" />
                <column name="CUST_ID" />
            </key-many-to-one>
            <key-property name="grpId" type="int">
                <column name="GRP_ID" />
            </key-property>
        </composite-id>
....


Of course, as is, finding groups by the ID listed above doesn't work.

Any help, ideas, or a better way of doing things (besides redesigning the schema, which is out of my control and is impossible. Trust me) is appreciated.

Disclaimers: I own the beginning hibernate book. It has a three sentence section on composite ids. Don't bother asking why the DA's love composite indexes. I don't know. I also read the manual on composite id's. I also have searched the forums and google extensively. I know i'm probably not the only person that has had this problem. I apologize if this is a stupid question, just answer it or tell me where to look for the answer. I am capable of reading and comprehending. I apologize if you read this and it makes you angry. I apologize if you read this and you feel I wasted part of your life.

Thank you


Last edited by cj91 on Fri Apr 03, 2009 5:02 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 5:01 pm 
Newbie

Joined: Thu Oct 09, 2008 12:51 pm
Posts: 7
Well well... Humility is what makes a man, so here I am to apologize. And i'm posting to help anyone who might tread down this path

I did figure this out. I changed this:
Code:
<class name="hibernate.generated.Group" table="GRP" >
        <composite-id name="id" class="hibernate.generated.GroupId">
            <key-many-to-one name="clntId" class="hibernate.generated.Client">
                <column name="CLNT_ID" />
                <column name="CUST_ID" />
            </key-many-to-one>
            <key-property name="grpId" type="int">
                <column name="GRP_ID" />
            </key-property>
        </composite-id>


to this:

Code:
<class name="hibernate.generated.Group" table="GRP" >
        <composite-id name="id" class="hibernate.generated.GroupId">
            <key-many-to-one name="clntId" class="hibernate.generated.Client">
                <column name="CUST_ID" />
                <column name="CLNT_ID" />
            </key-many-to-one>
            <key-property name="grpId" type="int">
                <column name="GRP_ID" />
            </key-property>
        </composite-id>


and it works. Good luck and happy hibernating!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.