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

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

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 @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.

Environment

None

Status

Assignee

Davide D'Alto

Reporter

Dimitrij Drus

Components

Fix versions

Affects versions

5.1.0.Final

Priority

Major