-->
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: Composite key mapping problem
PostPosted: Wed Dec 03, 2008 10:37 am 
Newbie

Joined: Tue Nov 11, 2008 8:19 am
Posts: 11
Hi all,
This is my first post in the hibernate forum. Hope ill get the answer for my question.

Hibernate version --- 3.3.1
Database --- Ms SQL server 2005

I am having a legacy table called "RELATN" with a composite key consists of five columns. 1,2,3,4 and 5 in the following list.

1 CLNO (P) --- int
2 PLANNO (P) --- char
3 RELATION (P) --- tinyint
4 DEEDTYPE (P) --- int
5 RelationSeqNo (P) --- int
5 BENFCPC --- money
7 LASTUPD --- varchar
8 CreatorId --- char
9 CreatorTypeCode --- tinyint
10 CreatedDate datetime
11 PseudoBeneficiaryId --- smallint
12 AssigneeInterestPct --- money
13 NomineeInterestPct --- money
14 RelatnStat tinyint
15 RoleIndicator --- tinyint
16 BenefitIndicator --- tinyint

I used <composite-ID>. Here is my RELATN.hbm.xml

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

<hibernate-mapping>

   <class name="com.tcs.Nulamda.hibernate.RELATN.java" table="RELATN">
      <composite-id class="com.tcs.Nulamda.hibernate.RELATN_PK"
         name="relatn_pk" access="property">
         <key-property name="CLNO" type="integer"></key-property>
         <key-property name="DEEDTYPE" type="integer"></key-property>
         <key-property name="PLANNO" type="string"></key-property>
         <key-property name="RELATION" type="byte"></key-property>
         <key-property name="RelationSeqNo" type="integer"></key-property>
      </composite-id>

      <property name="AssigneeInterestPct" type="big_decimal"></property>
      <property name="BenefitIndicator" type="byte"></property>
      <property name="BENFCPC" type="big_decimal"></property>
      <property name="CreatorTypeCode" type="byte"></property>
      <property name="NomineeInterestPct" type="big_decimal"></property>
      <property name="PseudoBeneficiaryId" type="byte"></property>
      <property name="RelatnStat" type="byte"></property>
      <property name="RoleIndicator" type="byte"></property>

   </class>
</hibernate-mapping>


And here is the java class ...RELATN.java
Code:
package com.tcs.Nulamda.hibernate;

import java.math.BigDecimal;

public class RELATN {
   
   //All Getter and setter except for the composite primary keys
   //its all in the RELATN_PK class
   
   private BigDecimal AssigneeInterestPct;
   private byte BenefitIndicator;
   private BigDecimal BENFCPC;
   private byte CreatorTypeCode;
   private BigDecimal NomineeInterestPct;
   private byte PseudoBeneficiaryId;
   private byte RelatnStat;
   private byte RoleIndicator;
   
   //Variable for composite key class
   
   private RELATN_PK relatn_pk;
   
   public RELATN() {
      // TODO Auto-generated constructor stub
   }
   
   public BigDecimal getAssigneeInterestPct() {
      return AssigneeInterestPct;
   }
   public void setAssigneeInterestPct(BigDecimal assigneeInterestPct) {
      AssigneeInterestPct = assigneeInterestPct;
   }
   public byte getBenefitIndicator() {
      return BenefitIndicator;
   }
   public void setBenefitIndicator(byte benefitIndicator) {
      BenefitIndicator = benefitIndicator;
   }
   public BigDecimal getBENFCPC() {
      return BENFCPC;
   }
   public void setBENFCPC(BigDecimal benfcpc) {
      BENFCPC = benfcpc;
   }
   public byte getCreatorTypeCode() {
      return CreatorTypeCode;
   }
   public void setCreatorTypeCode(byte creatorTypeCode) {
      CreatorTypeCode = creatorTypeCode;
   }
   public BigDecimal getNomineeInterestPct() {
      return NomineeInterestPct;
   }
   public void setNomineeInterestPct(BigDecimal nomineeInterestPct) {
      NomineeInterestPct = nomineeInterestPct;
   }
   public byte getPseudoBeneficiaryId() {
      return PseudoBeneficiaryId;
   }
   public void setPseudoBeneficiaryId(byte pseudoBeneficiaryId) {
      PseudoBeneficiaryId = pseudoBeneficiaryId;
   }
   public byte getRelatnStat() {
      return RelatnStat;
   }
   public void setRelatnStat(byte relatnStat) {
      RelatnStat = relatnStat;
   }
   public byte getRoleIndicator() {
      return RoleIndicator;
   }
   public void setRoleIndicator(byte roleIndicator) {
      RoleIndicator = roleIndicator;
   }
   public RELATN_PK getRelatn_pk() {
      return relatn_pk;
   }
   public void setRelatn_pk(RELATN_PK relatn_pk) {
      this.relatn_pk = relatn_pk;
   }

}


And here is the [b]RELATN_PK


Code:
public class RELATN_PK implements Serializable {

   private static final long serialVersionUID = 1L;
   
   private int CLNO;
   private int DEEDTYPE;
   private String PLANNO;
   private  byte RELATION;
   private int RelationSeqNo;
   
   public RELATN_PK() {
      // TODO Auto-generated constructor stub
   }
   
   public int getCLNO() {
      return CLNO;
   }
   public void setCLNO(int clno) {
      CLNO = clno;
   }
   public int getDEEDTYPE() {
      return DEEDTYPE;
   }
   public void setDEEDTYPE(int deedtype) {
      DEEDTYPE = deedtype;
   }
   public String getPLANNO() {
      return PLANNO;
   }
   public void setPLANNO(String planno) {
      PLANNO = planno;
   }
   public int getRELATION() {
      return RELATION;
   }
   public void setRELATION(byte relation) {
      RELATION = relation;
   }
   public int getRelationSeqNo() {
      return RelationSeqNo;
   }
   public void setRelationSeqNo(int relationSeqNo) {
      RelationSeqNo = relationSeqNo;
   }
   
   /*
    * The identifier class must override equals() and hashCode() and implement.
      Serializable. The disadvantage of this approach is quite obviouscode
      duplication.
    * */
   
   @Override
   public boolean equals(Object other) {
      if (this == other)return true;
      if ( !(other instanceof RELATN_PK))return false;
      final RELATN_PK relatn_PK = (RELATN_PK) other;
      if (!( relatn_PK.getCLNO() == getCLNO()) ) return false;
      if (!( relatn_PK.getDEEDTYPE() == getDEEDTYPE()) ) return false;
      if (!( relatn_PK.getPLANNO() == getPLANNO()) ) return false;
      if (!( relatn_PK.getRELATION() == getRELATION()) ) return false;
      if (!( relatn_PK.getRelationSeqNo() == getRelationSeqNo()) ) return false;
      return true;
   }
   
   @Override
   public int hashCode() {
      int result;
      result = getPLANNO().hashCode();
      result = 29 * result;
      return result;
   }
}


If i remove the RELATN.hbm.xml path in the hibernate.cfg.xml file. Everythings works fine. So can you please tell me what is the problem.

_________________
Vinoth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 3:21 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
What is the problem when the mapping file is there? Are you getting an error message? If so, please post.

As to why it goes away when you remove the hbm.xml file from the hibernate config file... I suspect that is due to it no longer loading a problematic mapping file. You say everything is ok... does that mean you can use that object? If so, then I don't see how with what you have posted.

Please provide more detail on the problem...

-Paul


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2008 1:53 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
vinoth4s, the name of the class must not contain ".class" or ".java". It's only the fully qualified name. So, you should remove the ".class" from the name attribute on this mapping.

It should be like this

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

<hibernate-mapping>

   <class name="com.tcs.Nulamda.hibernate.RELATN" table="RELATN">
      <composite-id class="com.tcs.Nulamda.hibernate.RELATN_PK" name="relatn_pk" access="property">
         <key-property name="CLNO" type="integer"></key-property>
         <key-property name="DEEDTYPE" type="integer"></key-property>
         <key-property name="PLANNO" type="string"></key-property>
         <key-property name="RELATION" type="byte"></key-property>
         <key-property name="RelationSeqNo" type="integer"></key-property>
      </composite-id>

      <property name="AssigneeInterestPct" type="big_decimal"></property>
      <property name="BenefitIndicator" type="byte"></property>
      <property name="BENFCPC" type="big_decimal"></property>
      <property name="CreatorTypeCode" type="byte"></property>
      <property name="NomineeInterestPct" type="big_decimal"></property>
      <property name="PseudoBeneficiaryId" type="byte"></property>
      <property name="RelatnStat" type="byte"></property>
      <property name="RoleIndicator" type="byte"></property>

   </class>
</hibernate-mapping>


As a suggestion, you could remove the "type" attribute to every column, it's inferred automatically from the class itself, it will make the mapping less verbose and easier to read, you should use them only when the type it's not determinable from the class, ex. java.util.Date could be saved date, time, timestamp.

regards,


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.