#1740 and #1744 Don't include the discriminator column on Inheritance query on a type that has no sub-types

Fix for #1740
This commit is contained in:
rob bygrave
2019-06-30 21:52:01 +12:00
parent eba521563a
commit 8ffc9ede85
9 changed files with 140 additions and 14 deletions
@@ -74,7 +74,7 @@ class AssocOneHelpRefInherit extends AssocOneHelp {
@Override
void appendSelect(DbSqlContext ctx, boolean subQuery) {
if (!subQuery) {
if (!subQuery && inherit.hasChildren()) {
// add discriminator column
String relativePrefix = ctx.getRelativePrefix(property.getName());
String tableAlias = ctx.getTableAlias(relativePrefix);
@@ -6,8 +6,8 @@ import io.ebeaninternal.server.deploy.id.IdBinder;
import io.ebeaninternal.server.deploy.parse.DeployInheritInfo;
import io.ebeaninternal.server.query.SqlTreeProperties;
import java.lang.reflect.Modifier;
import javax.persistence.PersistenceException;
import java.lang.reflect.Modifier;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -175,6 +175,16 @@ public class InheritInfo {
return children;
}
/**
* Return true if this node has children.
* <p>
* When an inheritance node has no children then we don't need
* the discriminator column as the type is effectively known.
*/
public boolean hasChildren() {
return !children.isEmpty();
}
/**
* Get the bean property additionally looking in the sub types.
*/
@@ -200,7 +210,6 @@ public class InheritInfo {
for (InheritInfo childInfo : children) {
selectProps.add(childInfo.descriptor.propertiesLocal());
childInfo.addChildrenProperties(selectProps);
}
}
@@ -209,9 +218,10 @@ public class InheritInfo {
* Return the associated InheritInfo for this DB row read.
*/
public InheritInfo readType(DbReadContext ctx) throws SQLException {
String discValue = ctx.getDataReader().getString();
return readType(discValue);
if (!hasChildren()) {
return this;
}
return readType(ctx.getDataReader().getString());
}
/**
@@ -327,7 +337,6 @@ public class InheritInfo {
* Return the derived where for the discriminator.
*/
public String getWhere() {
return where;
}
@@ -199,9 +199,7 @@ public class DeployInheritInfo {
public String getWhere() {
List<Object> discList = new ArrayList<>();
appendDiscriminator(discList);
return buildWhereLiteral(discList);
}
@@ -543,10 +543,9 @@ class SqlTreeNodeBean implements SqlTreeNode {
}
if (readId) {
if (!subQuery && inheritInfo != null) {
if (!subQuery && inheritInfo != null && inheritInfo.hasChildren()) {
ctx.appendColumn(inheritInfo.getDiscriminatorColumn());
}
appendSelectId(ctx, idBinder.getBeanProperty());
}
appendSelect(ctx, subQuery, properties);
@@ -0,0 +1,18 @@
package org.tests.aggregateformula;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.Table;
@Entity
@Inheritance
@Table(name = "iaf_segment")
@DiscriminatorColumn(name = "ptype")
public class IAFBaseSegment {
@Id
private Long id;
}
@@ -0,0 +1,30 @@
package org.tests.aggregateformula;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.ManyToOne;
@Entity
@Inheritance
@DiscriminatorValue("target")
public class IAFPartialSegment extends IAFBaseSegment {
private final long segmentIdZat;
@ManyToOne(optional = false)
private final IAFSegmentStatus status;
public IAFPartialSegment(long segmentIdZat, IAFSegmentStatus status) {
this.segmentIdZat = segmentIdZat;
this.status = status;
}
public long getSegmentIdZat() {
return segmentIdZat;
}
public IAFSegmentStatus getStatus() {
return status;
}
}
@@ -0,0 +1,35 @@
package org.tests.aggregateformula;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "iaf_segment_status")
public class IAFSegmentStatus {
@Id
private long id;
private String name;
public IAFSegmentStatus(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,39 @@
package org.tests.aggregateformula;
import io.ebean.DB;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestAggregateInheritFormula {
@Test
public void test() {
IAFSegmentStatus st0 = new IAFSegmentStatus("st0");
IAFSegmentStatus st1 = new IAFSegmentStatus("st1");
IAFPartialSegment p0 = new IAFPartialSegment(108, st0);
IAFPartialSegment p1 = new IAFPartialSegment(108, st1);
IAFPartialSegment p2 = new IAFPartialSegment(109, st0);
DB.saveAll(Arrays.asList(st0, st1, p0, p1));
LoggedSqlCollector.start();
List<IAFPartialSegment> segments =
DB.find(IAFPartialSegment.class)
.select("segmentIdZat, min(status)")
.where().eq("segmentIdZat", 108L)
.findList();
final List<String> sql = LoggedSqlCollector.stop();
assertThat(segments).hasSize(1);
assertThat(sql.get(0)).contains("select t0.segment_id_zat, min(t0.status_id) from iaf_segment t0 where t0.ptype = 'target' and t0.segment_id_zat = ? group by t0.segment_id_zat");
}
}
@@ -23,7 +23,6 @@ import static org.assertj.core.api.Assertions.assertThat;
public class TestQuerySingleAttribute extends BaseTestCase {
@Test
public void exampleUsage() {
@@ -236,8 +235,7 @@ public class TestQuerySingleAttribute extends BaseTestCase {
.select("more");
query.findSingleAttributeList();
assertThat(sqlOf(query)).contains("select distinct t0.more from rawinherit_parent t0 where t0.type = 'A' ");
assertThat(sqlOf(query)).contains("select distinct t0.more from rawinherit_parent t0 where t0.type = 'A'");
}
@Test