• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EclipseLink inheritance bug?: super class instantiated instead of sub class

 
Ranch Hand
Posts: 106
Hibernate Python MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have three tables:

Contacts
Persons
Players

Persons extends Contacts, Players extends Persons by identifying relationship (simple shared ID across all tables).

These are my entity classes (relevant parts only):



As you can see the Contact class is abstract, contact entities shouldn't be instantiatable. Persons however should, like players. I execute the following code on an EntityManager:

The first two finds are okay, both return Person instances as expected.

However, the third also returns a Person instance, which is wrong, giving an exception:

What's wrong? Is my code incorrect? Is this an EclipseLink bug?

I'm not sure about it, but shouldn't the working two objects be two different objects? The hashcode displayed hints that these are the same, so maybe my hashCode and equals are wrong?

You can find a standalone JavaSE/HSQLDB SSCE app here:
http://www.kawoolutions.com/media/persons.zip

I'd be glad if anybody could comment on this.

Karsten
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are getting the same Entity instance because you are querying for the Id = 1 in all three queries. Each entity instance in the hierarchy needs its own Id.
--Gordon
 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate Python MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The hierarchy was designed like that on purpose, they all share the same ID. Why should they need their own IDs? If that's the way you design your DB, well, you're just putting redundancy into your DBs which is not what I'm going to do...

An ORM should really handle this model. If I query an ID for different classes, I want the ORM to return just that class' entity and not some super type. If I call it onto an abstract class, the ORM should return the nearest sub entity, if a discriminator is set or null if not.

Karsten
 
Gordon Yorke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are a couple issues here.
First the two 'successful' queries should be returning the same instance because you are querying for the same object. Queries in JPA are always Polymorphic if the class type is a member of a mapped inheritance hierarchy. Users map inheritance because they want to take advantage of polymorphic queries. Each Entity in the hierarchy gets its own Id not because of redundancy but because without it the finders would not be polymorphic as there would be multiple instances in the same hierarchy with the same Id and these instances would be ignored. If you do not want polymorphic queries then do not map the inheritance simply add the subtables as secondary tables. You will have to ensure you are using some identifier to make the entries in the root table unique.

Second, there does seem to be a bug in EclipseLink, that third query should have returned null. You should file a bug report for that.

--Gordon
 
All of the following truths are shameless lies. But what about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic