Uploaded image for project: 'Hibernate ORM'
  1. HHH-11005

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

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects versions: 5.0.1, 5.1.0, 5.2.1
    • Fix versions: 5.2.3
    • Components: hibernate-core
    • Labels:
      None
    • Environment:
      MySQL, Oracle, (all DBs?)

      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:

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

      and a back-link from Book to Library

      	@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)

        Attachments

          Issue links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: