-->
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: equals breaks on getClass() test
PostPosted: Thu May 05, 2005 11:35 pm 
Beginner
Beginner

Joined: Thu May 05, 2005 11:12 pm
Posts: 26
Hibernate version: 3
Name and version of the database you are using: FireFox

Hi. This seems like a simple problem but I can't find it specifically in the forums and guides:

I have written a Dao's for the standard stuff, and a Hibernate Dao test suite. In one of the tests I am retrieving a currency rate, updating the rate to something else, saving it, retrieving it again to a different local var and testing that the first one I retrieved equls() the second one. It should be because the souce and target currencies are the same, and the start and end dates of the currency rates are the same. Only the fx rate has been changed. However the equality test fails when CurrencyRate tests the two source currencies. It fails in the o.getClass() != this.getClass() part of the Currency equals() method, because one is a Currency object and the other is one of those Currency$$EnhancerByCGILIB$$... objects.

I am using accessors in CurrencyRate to avoid lazy loading problems. I can't think what else to do to fix this. Anyone help?

Salient atributes and equals method for Currency:

private String currencyCode;
private String description;

public boolean equals( Object o )
{
if ( this == o ) return true;
if ( o == null || o.getClass() != this.getClass() ) return false;

final Currency currency = (Currency) o;
if ( this.getCurrencyCode() != null ?
!this.getCurrencyCode().equals( currency.getCurrencyCode() ) :
currency.getCurrencyCode() != null ) return false;

return true;
}

Salient attributes and equals method for CurrencyRate:

private DateMidnight startDate;
private DateTime endDate;
private Currency sourceCurrency;
private Currency targetCurrency;
private BigDecimal rate;

public boolean equals( Object o )
{
if ( this == o ) return true;
if ( o == null || o.getClass() != this.getClass() ) return false;

final CurrencyRate currencyRate = (CurrencyRate) o;

if ( !this.getStartDate().equals( currencyRate.getStartDate() ) ) return false;
if ( !this.getEndDate().equals( currencyRate.getEndDate() ) ) return false;
if ( !this.getSourceCurrency().equals( currencyRate.getSourceCurrency() ) ) return false;
if ( !this.getTargetCurrency().equals( currencyRate.getTargetCurrency() ) ) return false;

return true;
}

Hibernate mapping for CurrencyRate is:
<class name="CurrencyRate" table="CURRENCY_RATE" lazy="true">

<id name="objectId" column="CURRENCY_RATE_ID" type="long">
<generator class="hilo">
<param name="table">OBJECT_ID</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
</id>

<property name="startDate" column="START_DATE" type="com.ilign.ppm.common.PersistentDateMidnight"/>
<property name="endDate" column="END_DATE" type="com.ilign.ppm.common.PersistentDateTime"/>
<property name="rate" column="rate" type="big_decimal"/>

<many-to-one name="sourceCurrency" class="Currency" column="SOURCE_CURRENCY_ID" cascade="none"/>
<many-to-one name="targetCurrency" class="Currency" column="TARGET_CURRENCY_ID" cascade="none"/>
</class>

and for Currency is:
<class name="Currency" table="CURRENCY" lazy="true">

<id name="objectId" column="CURRENCY_ID" type="long">
<generator class="hilo">
<param name="table">OBJECT_ID</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
</id>

<property name="currencyCode" column="CURRENCY_CODE" type="string"/>
<property name="description" column="DESCRIPTION" type="string"/>
</class>


Top
 Profile  
 
 Post subject: equals
PostPosted: Thu Aug 18, 2005 4:11 am 
Beginner
Beginner

Joined: Tue May 10, 2005 4:18 am
Posts: 29
Use "instanceof" instead.


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.