-->
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: Subclasses and ClassCastException
PostPosted: Tue Mar 30, 2004 10:21 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
Hi all!
I'm trying to save a Subclass but I'm getting a ClassCastException.
these are my classes:
Code:

public class Tariff implements Serializable
{
   private ClientType clientType;
   private Calendar fromDate;
   private String name;
   private Double markUp;

//getters, setters, equals(), hashCode()...
}

public class CommissionableTariff extends Tariff
{
   private Double commission;
//getters, setters
}

public class NetTariff extends Tariff
{
//getters, setters
}



this is my hiberntate mapping:


Code:

<class name="es.tariff.Tariff" discriminator-value="T">
    <composite-id>
       <key-many-to-one name="clientType" class="es.agency.ClientType" column="clientType"/>
       <key-property name="fromDate" type="date"/>
   </composite-id>
   <discriminator column="type" type="character" />
   <property name="name" type="string"/>
   <property name="markUp" type="double" length="12"/>   
   
   <subclass name="es.tariff.NetTariff"
       discriminator-value="N" dynamic-update="true"
       dynamic-insert="true">
    </subclass>
   
    <subclass name="es.tariff.CommissionableTariff"
       discriminator-value="C" dynamic-update="true"
       dynamic-insert="true">
       <property name="commission" type="double" length="12"/>
    </subclass>   
</class>


Is this mapping right?
So this is the java code where I try to save the new instance of a NetTariff:

Code:

ClientType clientType = (ClientType)HibernateSession.currentSession().load(ClientType.class, new Long(rq.getClientTypeCode()));
         Calendar fromDate = rq.getTariffFromDate();
         Double markUp = new Double(NumberUtils.getDouble(rq.getTariffMarkUpInt(),rq.getTariffMarkUpDec()));
         String name = rq.getTariffName();

NetTariff nTariff = new NetTariff();
            nTariff.setClientType(clientType);
            nTariff.setFromDate(fromDate);
            nTariff.setMarkUp(markUp);
            nTariff.setName(name);
            
            HibernateSession.currentSession().saveOrUpdate(nTariff);
            HibernateSession.currentSession().flush();



And after saving I get a class cast exception...
Why is this?

Thank you very much in advance!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 10:39 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
The thing is that I'm also saving other objects with subclasses in other modules of the application and I'm not having this problem, can it be a mapping problem?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 2:29 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
are you sure the type for FromDate is ok?

are equals & hashcode well written?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 3:22 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
delpouve wrote:
are you sure the type for FromDate is ok?

are equals & hashcode well written?

Thank you for your response!
Here are my equals and hashCode:

Code:
public boolean equals( Object o )
   {
      if( o instanceof Tariff )
      {
         Tariff other = (Tariff) o;
         return (
               (this.clientType.equals(other.getClientType())) &&
               (this.fromDate.equals(other.getFromDate())));
      }

      return false;
   }

   public int hashCode()
   {
      return fromDate.hashCode() ^ clientType.hashCode();
   }


Are they ok?

I don't really think that the problem is in the fromDate, because I have many other classes with Calendar mapping to a date type, and there is no problem at all when saving or loading.


Top
 Profile  
 
 Post subject: Re: Subclasses and ClassCastException
PostPosted: Wed Mar 31, 2004 5:08 am 
Newbie

Joined: Mon Mar 22, 2004 5:23 am
Posts: 18
Location: Bangalore
rgarcia wrote:
Hi all!
I'm trying to save a Subclass but I'm getting a ClassCastException.
these are my classes:
Code:

public class Tariff implements Serializable
{
   private ClientType clientType;
   private Calendar fromDate;
   private String name;
   private Double markUp;

//getters, setters, equals(), hashCode()...
}

public class CommissionableTariff extends Tariff
{
   private Double commission;
//getters, setters
}

public class NetTariff extends Tariff
{
//getters, setters
}



this is my hiberntate mapping:


Code:

<class name="es.tariff.Tariff" discriminator-value="T">
    <composite-id>
       <key-many-to-one name="clientType" class="es.agency.ClientType" column="clientType"/>
       <key-property name="fromDate" type="date"/>
   </composite-id>
   <discriminator column="type" type="character" />
   <property name="name" type="string"/>
   <property name="markUp" type="double" length="12"/>   
   
   <subclass name="es.tariff.NetTariff"
       discriminator-value="N" dynamic-update="true"
       dynamic-insert="true">
    </subclass>
   
    <subclass name="es.tariff.CommissionableTariff"
       discriminator-value="C" dynamic-update="true"
       dynamic-insert="true">
       <property name="commission" type="double" length="12"/>
    </subclass>   
</class>


Is this mapping right?
So this is the java code where I try to save the new instance of a NetTariff:

Code:

ClientType clientType = (ClientType)HibernateSession.currentSession().load(ClientType.class, new Long(rq.getClientTypeCode()));
         Calendar fromDate = rq.getTariffFromDate();
         Double markUp = new Double(NumberUtils.getDouble(rq.getTariffMarkUpInt(),rq.getTariffMarkUpDec()));
         String name = rq.getTariffName();

NetTariff nTariff = new NetTariff();
            nTariff.setClientType(clientType);
            nTariff.setFromDate(fromDate);
            nTariff.setMarkUp(markUp);
            nTariff.setName(name);
            
            HibernateSession.currentSession().saveOrUpdate(nTariff);
            HibernateSession.currentSession().flush();



And after saving I get a class cast exception...
Why is this?

can u give the stack trace of the Exception??

Thank you very much in advance!!!


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.