OneToMany generated incorrect SQL where MapKey is on superclass and using InheritanceType.JOINED

Description

We have an entity class Product with InheritanceType.JOINED. This has a field inventoryCode.
We have an entity subclass Book extends Product.

We have an entity class Library. There is a OneToMany from Library to Book:

and a back-link from Book to Library

Operations trying to access this Map at runtime (Map.size(), entrySet(), etc) fall over with an SQLGrammarException because MapBinder is trying to look for the column inventoryCode on the table for Book rather than on the joined table for the superclass (example stack trace below). As might be imagined, this only happens for the JOINED inheritance type.

I have a test case which I will attach shortly.

ERROR: Unknown column 'booksoninv0_.INVCODE' in 'field list'
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:434)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:186)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:682)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2142)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:567)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:249)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:563)

Environment

MySQL, Oracle, (all DBs?)

Activity

Show:
Vlad Mihalcea
August 9, 2016, 2:07 PM

After discussing with the team, we concluded that we need to go through the hierarchy and resolve the property. I changed the tests to break each inheritance into its own test class because it was difficult to follow it in the current form. I'll also have to think if the fix is the best way to do it.

Vlad Mihalcea
August 9, 2016, 2:14 PM
Edited

For instance, the current solution uses a secondary select when we already have the join. We just have to provide the proper alias so that Hibernate finds the Map key. But after some investigation, I guess that using the JOIN is not that simple since we don't know the joined table alias when building the map index.

Vlad Mihalcea
August 10, 2016, 5:42 AM

Applied PR upstream after refactoring tests and removing code duplication

Vlad Mihalcea
August 10, 2016, 10:23 AM

, you might be interested in this issue for 5.0 and 5.1

Richard Barnes
August 22, 2016, 5:50 AM

Vlad, in response to your earlier query, yes I have signed the CLA - sorry for the delay in responding.

Thanks for your help and expertise in taking this issue further than I was able to!

Richard

Assignee

Unassigned

Reporter

Richard Barnes

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure