Skip to content

Commit

Permalink
Fix for #1522
Browse files Browse the repository at this point in the history
  • Loading branch information
kurbaniec committed Nov 28, 2024
1 parent b5c7ae6 commit a66aaa8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +21,6 @@
import java.util.NavigableSet;

/**
*
* @author Christian Beikov
* @since 1.0.0
*/
Expand Down Expand Up @@ -98,7 +98,7 @@ public <X extends SelectBuilder<X>> 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");
Expand All @@ -114,4 +114,12 @@ static boolean hasSubFetches(NavigableSet<String> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Expand Down Expand Up @@ -231,9 +231,9 @@ public interface QuxView {
@IdMapping
Long getId();

@Mapping("value")
String getValue();
@Mapping("someValue")
String getSomeValue();

}

@Override
Expand All @@ -254,15 +254,15 @@ 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");
foo.setFoo(fooQux);
em.persist(foo);

Qux barQux = new Qux();
barQux.setValue("bar");
barQux.setSomeValue("bar");
em.persist(barQux);
Bar bar = new Bar();
bar.setName("Bar");
Expand Down Expand Up @@ -310,8 +310,8 @@ public void testFullSelection() {
EntityViewSetting<BaseView, CriteriaBuilder<BaseView>> 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<Base> builder = cbf.create(em, Base.class);

Collection<BaseView> bases = evm.applySetting(setting, builder).getResultList();
Expand All @@ -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
Expand All @@ -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<Container> builder = cbf.create(em, Container.class);

ContainerView container = evm.applySetting(setting, builder).getSingleResult();
Expand All @@ -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
Expand Down

0 comments on commit a66aaa8

Please sign in to comment.