• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

findByPrimary with different PK's is returning equals Entity Beans

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We have an Entity Bean with a Composite Primary Key and the PK class is composed by two fields:

public java.math.BigDecimal cdCategoria;
public java.math.BigDecimal cdItem;

(it was generated by XDoclet and we changed the implementation of hashCode to avoid collisions).

Then we create two instances of this PK:

PK1: PK2:
cdCategoria: 11 cdCategoria: 6
cdItem : 1996 cdItem : 2001

The statements are:

PKClass pk1 = new PKClass(new BigDecimal(11), new BigDecimal(1996));
PKClass pk2 = new PKClass(new BigDecimal(6), new BigDecimal(2001));

and call findByPrimary twice to get two instances of Entities.

ic1 = home.findByPrimaryKey(pk1);
ic2 = home.findByPrimaryKey(pk2);

The strange thing is that the Entity we got on the second statement had the same content as the first!
How can it be possible that two DIFFERENT PK's return two EQUALS entity beans?

I confirmed this by a log I did:

05/09/2005 18:09:37 - =======================
05/09/2005 18:09:37 - pk1.equals(pk2): false
05/09/2005 18:09:37 - =======================
05/09/2005 18:09:37 - PK1 is: [.11.1996.]; HashCode of PK is: 1101996
05/09/2005 18:09:37 - Entity PK returned is: [.11.1996.]; HashCode of the Entity PK is: 1101996
05/09/2005 18:09:37 - =======================
05/09/2005 18:09:37 - PK2 is: [.6.2001.]; HashCode of PK is: 602001
05/09/2005 18:09:37 - Entity PK returned is: [.11.1996.]; HashCode of the Entity PK is: 1101996
05/09/2005 18:09:37 - =======================
05/09/2005 18:09:37 - ic1.equals(ic2): true
05/09/2005 18:09:37 - =======================

As you see, pk1.equals(pk2) is false but the instances of the Entity Beans we found are equals!!!
It seems to me that the container is ignoring my implementation of hashCode on the PK Class and is executing
his own implementation. I think that this implementation consists basically of a sum of the fields hashCodes
in the PK Class and then uses it as a key to store the Entity instance on his Entity Pool, so, if you look
at these two instances of the PK Classes that I created, the sum the two fields are: 2007 in both instances!

I think it's an incorrect implementation done by the container!
Had anybody seen any report bug before?

    Bookmark Topic Watch Topic
  • New Topic