Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • 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
 
Gordon Yorke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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 MySQL Database Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic