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.
{code:java} @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 ... }
// from a e.g. test and having multiple corresponding documents in the DB, the following query will return the proper objects, but the subClass*Property will be null in all cases.
final TypedQuery<BaseClass> query = manager.createQuery("SELECT b FROM BaseClass b WHERE b.baseClassProperty='some value'", BaseClass.class); {code}
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.
|
|