This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes EclipseLink inheritance bug?: super class instantiated instead of sub class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "EclipseLink inheritance bug?: super class instantiated instead of sub class" Watch "EclipseLink inheritance bug?: super class instantiated instead of sub class" New topic
Author

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

Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

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


OCJP JavaSE 6 (86%)
Gordon Yorke
Greenhorn

Joined: Nov 16, 2009
Posts: 7
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

Joined: Jul 20, 2010
Posts: 106

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

Joined: Nov 16, 2009
Posts: 7
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
 
Don't get me started about those stupid light bulbs.
 
subject: EclipseLink inheritance bug?: super class instantiated instead of sub class
 
Similar Threads
Could not resolve a persistence unit corresponding to the persistence-context-ref-name
One to many Relation
Error: Entity <name> has no Id or EmbeddedId
Can't invoke a method on an -existing- bean (JEE)
Help with JPA