We're updating the issue view to help you get more done. 

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:

1 2 3 @OneToMany(mappedBy="library") @MapKey(name="inventoryCode") private Map<String,Book> booksOnInventory = new HashMap<String,Book>();

and a back-link from Book to Library

1 2 @ManyToOne private Library 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?)

Status

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

5.1.0
5.0.1
5.2.1

Priority

Major