diff --git a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/objectbuilder/ViewTypeObjectBuilder.java b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/objectbuilder/ViewTypeObjectBuilder.java index c0693aad50..d061b2c21c 100644 --- a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/objectbuilder/ViewTypeObjectBuilder.java +++ b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/objectbuilder/ViewTypeObjectBuilder.java @@ -9,6 +9,7 @@ import com.blazebit.persistence.ObjectBuilder; import com.blazebit.persistence.ParameterHolder; import com.blazebit.persistence.SelectBuilder; +import com.blazebit.persistence.view.impl.objectbuilder.mapper.AliasExpressionTupleElementMapper; import com.blazebit.persistence.view.impl.objectbuilder.mapper.TupleElementMapper; import com.blazebit.persistence.view.impl.proxy.ObjectInstantiator; import com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro; @@ -20,7 +21,6 @@ import java.util.NavigableSet; /** - * * @author Christian Beikov * @since 1.0.0 */ @@ -98,7 +98,7 @@ public > void applySelects(X queryBuilder) { for (int i = 0; i < mappers.length; i++) { TupleElementMapper mapper = mappers[i]; String attributePath = mapper.getAttributePath(); - if (attributePath != null && hasSubFetches(fetches, attributePath)) { + if (attributePath != null && (hasSubFetches(fetches, attributePath) || isInheritance(mapper, attributePath))) { mapper.applyMapping(queryBuilder, parameterHolder, optionalParameters, viewJpqlMacro, embeddingViewJpqlMacro, false); } else { queryBuilder.select("NULL"); @@ -114,4 +114,12 @@ static boolean hasSubFetches(NavigableSet fetches, String attributePath) String fetchedPath = fetches.ceiling(attributePath); return fetchedPath != null && fetchedPath.startsWith(attributePath) && (fetchedPath.length() == attributePath.length() || fetchedPath.charAt(attributePath.length()) == '.'); } + + static boolean isInheritance(TupleElementMapper mapper, String attributePath) { + // Should fetch discriminator column instead of selecting “NULL” + if (!attributePath.isEmpty()) { + return false; // Not a discriminator column + } + return mapper instanceof AliasExpressionTupleElementMapper && ((AliasExpressionTupleElementMapper) mapper).getAlias().endsWith("_class"); + } } diff --git a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/basic/InheritanceFetchesTest.java b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/basic/InheritanceFetchesTest.java index 83d20ad87e..436647d97a 100644 --- a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/basic/InheritanceFetchesTest.java +++ b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/basic/InheritanceFetchesTest.java @@ -159,14 +159,14 @@ public static class Qux { private Long id; @Column - private String value; + private String someValue; - public String getValue() { - return value; + public String getSomeValue() { + return someValue; } - public void setValue(String value) { - this.value = value; + public void setSomeValue(String value) { + this.someValue = value; } } @@ -231,9 +231,9 @@ public interface QuxView { @IdMapping Long getId(); - @Mapping("value") - String getValue(); - + @Mapping("someValue") + String getSomeValue(); + } @Override @@ -254,7 +254,7 @@ public void setUpOnce() { @Override public void work(EntityManager em) { Qux fooQux = new Qux(); - fooQux.setValue("foo"); + fooQux.setSomeValue("foo"); em.persist(fooQux); Foo foo = new Foo(); foo.setName("Foo"); @@ -262,7 +262,7 @@ public void work(EntityManager em) { em.persist(foo); Qux barQux = new Qux(); - barQux.setValue("bar"); + barQux.setSomeValue("bar"); em.persist(barQux); Bar bar = new Bar(); bar.setName("Bar"); @@ -310,8 +310,8 @@ public void testFullSelection() { EntityViewSetting> setting = EntityViewSetting.create(BaseView.class); setting.fetch("id"); setting.fetch("name"); - setting.fetch("foo.value"); - setting.fetch("bar.value"); + setting.fetch("foo.someValue"); + setting.fetch("bar.someValue"); CriteriaBuilder builder = cbf.create(em, Base.class); Collection bases = evm.applySetting(setting, builder).getResultList(); @@ -323,11 +323,11 @@ public void testFullSelection() { FooView foo = firstOrNull(bases, FooView.class); assertNotNull(foo); assertEquals("Foo", foo.getName()); - assertEquals("foo", foo.getFoo().getValue()); + assertEquals("foo", foo.getFoo().getSomeValue()); BarView bar = firstOrNull(bases, BarView.class); assertNotNull(bar); assertEquals("Bar", bar.getName()); - assertEquals("bar", bar.getBar().getValue()); + assertEquals("bar", bar.getBar().getSomeValue()); } @Test @@ -349,12 +349,12 @@ public void testContainerFullSelection() { setting.fetch("id"); setting.fetch("base1.id"); setting.fetch("base1.name"); - setting.fetch("base1.foo.value"); - setting.fetch("base1.bar.value"); + setting.fetch("base1.foo.someValue"); + setting.fetch("base1.bar.someValue"); setting.fetch("base2.id"); setting.fetch("base2.name"); - setting.fetch("base2.foo.value"); - setting.fetch("base2.bar.value"); + setting.fetch("base2.foo.someValue"); + setting.fetch("base2.bar.someValue"); CriteriaBuilder builder = cbf.create(em, Container.class); ContainerView container = evm.applySetting(setting, builder).getSingleResult(); @@ -363,11 +363,11 @@ public void testContainerFullSelection() { assertNotNull(container.getBase1()); FooView foo = (FooView) container.getBase1(); assertEquals("Foo", foo.getName()); - assertEquals("foo", foo.getFoo().getValue()); + assertEquals("foo", foo.getFoo().getSomeValue()); assertNotNull(container.getBase2()); BarView bar = (BarView) container.getBase2(); assertEquals("Bar", bar.getName()); - assertEquals("bar", bar.getBar().getValue()); + assertEquals("bar", bar.getBar().getSomeValue()); } @Test