Uploaded image for project: 'Hibernate OGM'
  1. OGM-1294

MongoDB - Properties of child object in an inheritance hierarchies are not loaded when coming from a relation

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.1.0.Final
    • Fix Version/s: 5.2.0.Alpha1
    • Component/s: mongodb
    • Labels:
      None

      Description

      In a polymorphic relation with an entity hierarchy using SINGLE_TABLE inheritance type, the properties of subclasses (which do not exist in the parent class) are not loaded if the query to retrieve the required objects acts on a parent class. It looks like the selector, created by Hibernate OGM for MongoDB extension, considers properties of a base class only. This works without any issue with any JPA provider for RDBMS databases.

      E.g.

      @Entity
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(name = "TYPE")
      public abstract class BaseClass {
          private String baseClassProperty;
      }
      
      @Entity
      @DiscriminatorValue(value = "A")
      public class SubClassA extends BaseClass {
         private String subClassAProperty;
         
         // further properties and methods ...
      }
      
      @Entity
      @DiscriminatorValue(value = "B")
      public class SubClassB extends BaseClass {
         private String subClassBProperty;
      
         // further properties and methods ...
      }
      
      @Entity
      public class BaseClassUser {
         @Id
         private Long id;
         
         @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
         private Set<BaseClass> values= new HashSet<>();
      }
      
      // from a e.g. test and having multiple corresponding documents in the DB, the following query will return the proper objects in the values relation of the loaded BaseClassUser object, but the subClass*Property will be null in all cases.
      
      final TypedQuery<BaseClassUser> query = manager.createQuery("SELECT u FROM BaseClassUser u WHERE u.='1'", BaseClassUser.class);
      

      You can find a fully working example in https://github.com/dadrus/jpa-unit to reproduce this issue. The affected test case is in https://github.com/dadrus/jpa-unit/blob/master/integration-test/base/src/main/java/eu/drus/jpa/unit/test/AbstractApplyCustomScripsTest.java (test1() method; line 70) With Hibernate OGM and MongoDB it can be run using https://github.com/dadrus/jpa-unit/blob/master/integration-test/jpa-mongodb-hibernate-ogm-test/src/test/java/eu/drus/jpa/unit/test/ApplyCustomScripsTest.java. The aforementioned line is disabled because of this issue. If enabled NPE is thrown with Hibernate OGM and MongoDB.

      If you run the same test but for a RDBMS (e.g. from https://github.com/dadrus/jpa-unit/blob/master/integration-test/jpa2.1-hibernate-test/src/test/java/eu/drus/jpa/unit/test/ApplyCustomScripsTest.java) everything is fine.

        Attachments

          Issue links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: