-->
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.  [ 5 posts ] 
Author Message
 Post subject: Class Cast Exception while using composit-id
PostPosted: Wed Dec 31, 2008 10:27 am 
Newbie

Joined: Tue Nov 11, 2008 8:19 am
Posts: 11
First of all, happy new year to everyone.

Dear all,

I am using Hibernate 3.0

I am getting the class cast exception. Please help me to solve this issue. I checked for the datatype of the attributes in the POJO, hbm.xml and also in the Database. I believe there is no problem with that.

There is a Tabe called RELATN, which have a composite key consist of 5 attributes.

RELATN.java
Code:
package nulamda.Bean;

import java.math.BigDecimal;
import java.sql.Timestamp;

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 short PseudoBeneficiaryId;
   private byte RelatnStat;
   private byte RoleIndicator;
   private Timestamp CreatedDate;
   private String CreatorId;

   //Variable for composite key class
   private RELATN_PK relatn_pk;
   
   private CLIENT client;
   
   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 short getPseudoBeneficiaryId() {
      return PseudoBeneficiaryId;
   }

   public void setPseudoBeneficiaryId(short 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 Timestamp getCreatedDate() {
      return CreatedDate;
   }

   public void setCreatedDate(Timestamp createdDate) {
      CreatedDate = createdDate;
   }

   public String getCreatorId() {
      return CreatorId;
   }

   public void setCreatorId(String creatorId) {
      CreatorId = creatorId;
   }

   public RELATN_PK getRelatn_pk() {
      return relatn_pk;
   }

   public void setRelatn_pk(RELATN_PK relatn_pk) {
      this.relatn_pk = relatn_pk;
   }

   public CLIENT getClient() {
      return client;
   }

   public void setClient(CLIENT client) {
      this.client = client;
   }
   
}


RELATN_PK.java

Code:
package nulamda.Bean;

import java.io.Serializable;

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) {
      try {
         
      
      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;
      } catch (Exception e) {
         System.out.println("inside the boolean class exception");
         return false;
      }
   }
   
   @Override
   public int hashCode() {
      int result = 0;
      try {
      result = getPLANNO().hashCode();
      result = (int) 29 * result;
      return result;
      } catch (Exception e) {
         System.out.println("inside the hashcode method exception");
         return result;
      }
   }
}


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="nulamda.Bean.RELATN" table="RELATN" lazy="false" >
      <composite-id class="nulamda.Bean.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="short"></property>
      <property name="RelatnStat" type="byte"></property>
      <property name="RoleIndicator" type="byte"></property>

   </class>
</hibernate-mapping>



The class getting the details

Code:
package nulamda.Support;

import java.util.List;
import nulamda.Bean.RELATN;
import nulamda.Bean.RELATN_PK;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class RelatnSupport {

   public void GetRelatnDetails()
   {
      
      Session session = HibernateSessionFactory.currentSession();
      Transaction transaction = session.beginTransaction();
      Criteria relatnCriteria = session.createCriteria(RELATN.class);
      RELATN_PK relatn_pk = new RELATN_PK();
      relatn_pk.setPLANNO("111111111x1");
      relatnCriteria.add(Restrictions.eq("relatn_pk", relatn_pk));
      List<RELATN> relatnList = relatnCriteria.list();
         
      System.out.println("size of the returned relatn list is : "+ relatnList.size());
      transaction.commit();

   }
}


Error message

Code:
package nulamda.Support;

import java.util.List;
import nulamda.Bean.RELATN;
import nulamda.Bean.RELATN_PK;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class RelatnSupport {

   public void GetRelatnDetails()
   {
      
      Session session = HibernateSessionFactory.currentSession();
      Transaction transaction = session.beginTransaction();
      Criteria relatnCriteria = session.createCriteria(RELATN.class);
      RELATN_PK relatn_pk = new RELATN_PK();
      relatn_pk.setPLANNO("111111111x1");
      relatnCriteria.add(Restrictions.eq("relatn_pk", relatn_pk));
      List<RELATN> relatnList = relatnCriteria.list();
         
      System.out.println("size of the returned relatn list is : "+ relatnList.size());
      transaction.commit();

   }
}


Please help me to solve the issue. Thanks in advance[/b]

_________________
Vinoth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 3:44 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
In RELATN_PK, the RELATION is byte, but the getter returns int.

public int getRELATION() {


Top
 Profile  
 
 Post subject: That problem is solved..But
PostPosted: Fri Jan 02, 2009 2:14 am 
Newbie

Joined: Tue Nov 11, 2008 8:19 am
Posts: 11
Hi That problem is resolved..thanks... But next one arised ... When i tried to add a restriction for any of the Atttributes in the RELATN_PK, i am getting the list with size 0. But for the corresponding search parameters, it shoud return list of size of more than one.

Code:
package nulamda.Support;

import java.util.List;
import nulamda.Bean.RELATN;
import nulamda.Bean.RELATN_PK;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class RelatnSupport {
   public void GetRelatnDetails(String Planno)

   {
      
      Session session = HibernateSessionFactory.currentSession();
      Transaction transaction = session.beginTransaction();
      Criteria relatnCriteria = session.createCriteria(RELATN.class);
      RELATN_PK objrelatn_pk = new RELATN_PK();
   
         
      objrelatn_pk.setPLANNO(Planno);

      System.out.println("objrelatn_pk.setPLANNO(Planno) : "+ Planno );
      relatnCriteria.add(Restrictions.like("relatn_pk", objrelatn_pk));
      List<RELATN> relatnList = relatnCriteria.list();
      System.out.println(" size of the returned relatn list is : "+ relatnList.size());
      
      transaction.commit();

   }
}


The code generated by the Hibernate

Hibernate: select this_.CLNO as CLNO0_, this_.DEEDTYPE as DEEDTYPE0_, this_.PLANNO as PLANNO0_, this_.RELATION as RELATION0_, this_.RelationSeqNo as Relation5_0_, this_.AssigneeInterestPct as Assignee6_7_0_, this_.BenefitIndicator as BenefitI7_7_0_, this_.BENFCPC as BENFCPC7_0_, this_.CreatorTypeCode as CreatorT9_7_0_, this_.NomineeInterestPct as Nominee10_7_0_, this_.PseudoBeneficiaryId as PseudoB11_7_0_, this_.RelatnStat as RelatnStat7_0_, this_.RoleIndicator as RoleInd13_7_0_ from RELATN this_ where (this_.CLNO like ? and this_.DEEDTYPE like ? and this_.PLANNO like ? and this_.RELATION like ? and this_.RelationSeqNo like ?)

Here i only set the values of Planno in the method, Not the others. Is that creating the problem ..? Or what might be the reason ..?[/code]

_________________
Vinoth


Last edited by vinoth4s on Fri Jan 02, 2009 2:28 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 2:25 am 
Newbie

Joined: Tue Nov 11, 2008 8:19 am
Posts: 11
I resolved this problem by switching over to the HQL.

Code:
   public void GetRelatnDetails(String Planno)
   {
      
      Session session = HibernateSessionFactory.currentSession();
      Transaction transaction = session.beginTransaction();
      
      List<RELATN> list = session.createQuery("[b]from RELATN as R where R.relatn_pk.PLANNO = '"+Planno+"'"[/b]).list();
      System.out.println(" !!!!!!!!!!!!!! @@@@@@@@@@@@@@ List of the Size is !!!!!!!!!!! @@@@@@@@@@ :"+ list.size());
      transaction.commit();
      
   }


But can anyone tell, what went wrong in the Criteria query ..?

_________________
Vinoth


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 9:45 am 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
Hello vinoth4s, the criteria query doesn't work because when you instantiate the RELATN_PK all it's properties are set to their default values, making this values the one that the query will have to look for, returning zero elements on your criteria query. You can exclude the default properties with this http://www.hibernate.org/hib_docs/v3/reference/en-US/html/querycriteria-examples.html.

regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.