-->
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.  [ 3 posts ] 
Author Message
 Post subject: 1 to many with composite id
PostPosted: Thu Jul 21, 2005 5:30 pm 
Beginner
Beginner

Joined: Thu Jul 21, 2005 10:28 am
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.0.x

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class
name="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"
table="CCC.AGNT_MSG">

<id name="id" type="long" unsaved-value="0">
    <column name="MSG_ID" sql-type="NUMBER" not-null="true" />
    <generator class="sequence">
           <param name="sequence">CCC.SEQ_AGNT_MSG</param>
    </generator>
</id>

<set name="recipients" cascade="save-update"table="MSG_RECIPIENT">
              <key column="MSG_ID" />
   <one-to-many class="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO" />
</set>

<property name="fromDate" type="date">
<column name="MSG_START_DATE" sql-type="DATE" not-null="false" />
</property>

<property name="toDate" type="date">
<column name="MSG_END_DATE" sql-type="DATE" not-null="false" />
</property>
      
<property name="messageText" type="string">
<column name="MSG_TEXT" sql-type="VARCHAR2(500)" not-null="false" />
</property>

<property name="createUser" type="string">
<column name="CREATE_USER_ID" sql-type="VARCHAR2(20)"
            not-null="false" />
</property>

<property name="createDate" type="date">
<column name="CREATE_DATE" sql-type="DATE" not-null="false" />
</property>

<property name="modUser" type="string">
<column name="UPDATE_USER_ID" sql-type="VARCHAR2(20)"
            not-null="false" />
</property>

<property name="modDate" type="date">
<column name="UPDATE_DATE" sql-type="DATE" not-null="false" />
</property>

<property name="categoryCode" type="string">
<column name="MSG_CATG_CD" sql-type="VARCHAR2(20)" not-null="false" />
</property>

<property name="behaviorCode" type="string">
<column name="MSG_BHVR_CD" sql-type="VARCHAR2(20)" not-null="false" />
</property>

<many-to-one name="messageCategoryDVO" column="MSG_CATG_CD"  class="com.starwood.saratoga.dataaccess.dvo.MessageCategoryDVO" not-null="true" insert="false" update="false"/>
<many-to-one name="messageBehaviorDVO" column="MSG_BHVR_CD"  class="com.starwood.saratoga.dataaccess.dvo.MessageBehaviorDVO" not-null="true" insert="false" update="false"/>   

</class>

<class name="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO"
      table="CCC.MSG_RECIPIENT">

<composite-id name="compositeKey" class="com.starwood.saratoga.dataaccess.dao.MessageRecipientCompositeKey">
<key-property name="messageId"   type="long"   column="MSG_ID"/>
<key-property name="recipientId" type="string" column="RECIPIENT_ID"/>
<key-property name="receiverCategoryCode" type="string" column="RCVR_CATG_CD"/>
</composite-id>   

<property name="recipientId" type="string" insert="false"   update="false" >
<column name="RECIPIENT_ID" sql-type="VARCHAR2(50)" not-null="true"/>
</property>

<property name="receiverCategoryCode" type="string" insert="false"   update="false" >
<column name="RCVR_CATG_CD" sql-type="VARCHAR2(20)" not-null="true"/>
</property>

<many-to-one name="recvCatgCd" column="RCVR_CATG_CD"  class="com.starwood.saratoga.dataaccess.dvo.ReceiverCategoryDVO" not-null="true" insert="false" update="false"/>

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
   Session session = null;
      try {
         System.out.println("AgentMessageDAO: create Transaction");
         Transaction tx = session.beginTransaction();
         System.out.println("AgentMessageDAO: create session");
         session = HibernateUtil.currentSession();
         System.out.println("AgentMessageDAO: saveOrUpdate: start");
         session.saveOrUpdate(agentMessageDVO);
         System.out.println("AgentMessageDAO: saveOrUpdate: finish: start tx commit");
         tx.commit();
         System.out.println("AgentMessageDAO: finish tx commit");
      } catch (HibernateException e) {
         log("AgentMessageDVO: saveOrUpdate: Hibernate Error Occured");
         e.printStackTrace();
      } finally {
         try {
            HibernateUtil.closeSession(session);
         } catch (Exception ignore) {
            log("AgentMessageDVO: saveOrUpdate: Session Close Error Occured");
            ignore.printStackTrace();
         }
      }



Full stack trace of any exception that occurs: N/A now

Name and version of the database you are using: Oracle 9i

The generated SQL (show_sql=true): N/A for now

Debug level Hibernate log excerpt: N/A for now

Composite Key Code
Code:
public class MessageRecipientCompositeKey  extends CompositeKey {
   
private long messageId;
private String recipientId;
private String receiverCategoryCode;
   
public int hashCode() {
       int tmp = 0;
       tmp = (messageId + recipientId + receiverCategoryCode).hashCode();
       return tmp;
    }

   /**
    * @return Returns the messageId.
    */
   public long getMessageId() {
       return messageId;
   }
   /**
    * @param ldapID The ldapID to set.
    */
   public void setMessageId(long messageId) {
       this.messageId = messageId;
   }
   
   /**
    * @return Returns the recipientId.
    */
   public String getRecipientId() {
       return recipientId;
   }
   /**
    * @param recipientId The recipientId to set.
    */
   public void setRecipientId(String recipientId) {
       this.recipientId = recipientId;
   }
   
   /**
    * @return Returns the receiverCategoryCode.
    */
   public String getReceiverCategoryCode() {
       return receiverCategoryCode;
   }
   /**
    * @param receiverCategoryCode The receiverCategoryCode to set.
    */
   public void setReceiverCategoryCode(String receiverCategoryCode) {
       this.receiverCategoryCode = receiverCategoryCode;
   }

}


I apologize for asking about this question since I am a Hibernate newbie, and I just bought Hibernate in Action only moments ago, and I will start reading it.

I know this question has been asked a few times, so I figured it couldn't hurt to ask again.

From the data provided, I have a table and by itself I can add data to it just fine. Now, I have a child table, and this will be a one-to-many relationship. A MSG_ID is the PK of the parent table set with a sequence. The child table has a composite key of 3 fields ... one of which is MSG_ID which should come from the parent table.

So, is this possible to do?
Is there a special mapping I can use?
Do I need to create a special composite key class?
Can I get the MSG_ID after the first table is written and use another
saveOrUpdate to write my child table in a transaction?
Which is the best preferred method?

Thanks for any help and sorry to be a pain!

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 5:57 pm 
Newbie

Joined: Tue Jun 28, 2005 11:11 am
Posts: 13
Location: St. Louis, Missouri, USA
So, is this possible to do?

Yes.

Is there a special mapping I can use?

No mapping files for your composite-key POJOs. In your value object mapping you'll want to say something like
Code:
     <composite-id
           name="YourPoJosPrimaryKeyVariable"
            class="CompositeKeyClass"  >

         <key-property
            name="primaryKeyPropertyOne"
            type="string"
            column="TABLE_ONE"
         />
         
         <key-property
            name="primaryKeyPropertyTwo"
            type="string"
            column="TABLE_TWO"
         />                     

        </composite-id>


In this example, the variables in the CompositeKeyClass will get their data from the TABLE_ONE and TABLE_TWO columns that are in whatever table this POJO would use.

Do I need to create a special composite key class? Yes
Can I get the MSG_ID after the first table is written and use another
saveOrUpdate to write my child table in a transaction?

I don't know what would happen with this. Hibernate should be smart enough to let this happen but don't take my word on it.

Which is the best preferred method?

I don't know honestly. The docs are horrible on composite-key examples that span multiple columns but that isn't a suprise since they inform you every chance they get that surrogate keys are better (easier probably).

HTH
[/code]


Top
 Profile  
 
 Post subject: 2 tables not three
PostPosted: Fri Jul 22, 2005 1:41 am 
Beginner
Beginner

Joined: Thu Jul 21, 2005 10:28 am
Posts: 21
Quote:
In this example, the variables in the CompositeKeyClass will get their data from the TABLE_ONE and TABLE_TWO columns that are in whatever table this POJO would use.


I'm not sure if I made myself clear before ... I do only have two tables: the parent and the child table. So, I have a situation like this

Table1=id is the Primary Key and generated by an Oracle Sequence
id=1 date=somedate text=sometext
id=2 date=somedate text=sometext
id=3 date=somedate text=sometext

Table2 only has 3 fields which make up the composite key
id is the parent id, but it is also part of this composite key
id=1 recId='Jim' code=1
id=1 recId='Cork' code=3
id=2 recId='Tim' code=1
id=2 recId='Admin' code=2

So, you can see how the id's in both tables ... and I was wondering with Hibernate 2.0.x if I can map this out so I can write a new row in Table1 and have it automatically write out the matching records in Table2.

According to the Wrox Hibernate book, this is would have been possible if I had been able to create a single primary key and then made these 3 fields a unique index.

Anyway, thanks for the help, maybe someone else has done the same thing I am doing.


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