-->
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: 2 components in a composite key?
PostPosted: Mon Jul 18, 2005 12:41 pm 
Newbie

Joined: Mon Jul 18, 2005 12:17 pm
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3

Name and version of the database you are using:DB2 7.2

Is it possible to have a composite id that is defined by 2 components?

Specifically, I have a POJO that contains 2 UserTypes (an Identifier and a Duration object). The identifier holds 2 Strings, id and subId. The Duration holds an effectiveDate and a terminationDate. The database table is defined so that a row is uniquely identified by the 3 fields id, subId, and effectiveDate.

This brings up 2 problems for me. 1) I need to be able to define the composite id using 2 classes. Something along the lines of:
Code:
<composite-id name="identifier" class="GroupIdentifier">
    <key-property name="id" column="ID" type="string" />
    <key-property name="subId" column="SUB_ID" type="string" />
</composite-id>
<composite-id name="effectivePeriod" class="Duration">
    <key-property name="effectiveDate" column="EFF_DATE" type="date" />
</composite-id>

I know that the mapping does not work this way but is there anyway to get it to function in a similar way?

2) Part of the Duration object is part of the composite id and part is not. Will I be able to map half of the component in the composite id and the other half outside the key?

Code:
<composite-id name="effectivePeriod" class="Duration">
    <key-property name="effectiveDate" column="EFF_DATE" type="date" />
</composite-id>
<component name="effectivePeriod" class="Duration">
    <property name="effectiveDate" column="EFF_DATE" type="date" />
<component>


Thanks for any insight you can provide!
Erik Raisanen


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 11:41 am 
Newbie

Joined: Tue Jun 28, 2005 11:11 am
Posts: 13
Location: St. Louis, Missouri, USA
I have an application where some of the tables have 5 element composit-ids, some have 3 element, and some have 2. Our DBAs mandate that a primary key may not be a surrogate and must describe the data (sequence numbers are not allowed, if it is at all possible).

Hibernate is really easy with this. Instead of a long winded paragraph, here is some code.

Primary key example:
Code:
public class FinNumRteNumPK implements Serializable {
   
   private String financeNumber = "";
   private String routeNumber = "";
   
   public FinNumRteNumPK() {
      super();
   }
// getter and setters


hibernate mapped example
Code:
public class TestVO  implements Serializable {
   

   private FinNumRteNumPK primarykey = null;
   private String something = "";
      
   public TestVO () {
      super();
      primarykey = new FinNumRteNumPK ();

   }
}
//getter and setters


Hibernate mapping
Code:
<hibernate-mapping>

   <class name="TestVO "
      table="MY_TABLE">

      <composite-id
           name="primarykey"
            class="FinNumRteNumPK "  >

         <key-property
            name="financeNumber"
            type="string"
            column="FIN_NBR"
         />
         
         <key-property
            name="routeNumber"
            type="string"
            column="RTE_NBR"
         />                     

        </composite-id>

      <property name="something "
         type="string"
         update="true"
         insert="true"
         access="property"
         column="SOMETHING"
         lazy="false"
      >
      </property>   


If I wanted to search based on this primary key

Code:
StringBuffer sb = new StringBuffer("from TestVo p where p.primarykey.routeNumber = :route ");
            sb.append("and p.primarykey.financeNumber = :finance ");
            
            Query query = session.createQuery(sb.toString());
            query.setString("finance", finance);
            query.setString("route", route);
query.list();


That's it. To recap, I have my primary key java class, my value object class, my hibernate mapping for that value object.


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.