#1837 (#1740 and #1744) - Inheritance on both sides of @ManyToOne

This change also relates back to #1740 and #1744 and changes the way that issue was fixed (by SqlTreeNodeBean on partially populated non-id inheritance bean).
This commit is contained in:
rob bygrave
2019-10-13 11:46:11 +13:00
parent a474880735
commit f24b4dba71
10 changed files with 21 additions and 42 deletions
@@ -21,9 +21,7 @@ class AssocOneHelpRefInherit extends AssocOneHelp {
@Override
void loadIgnore(DbReadContext ctx) {
property.targetIdBinder.loadIgnore(ctx);
if (inherit.hasChildren()) {
ctx.getDataReader().incrementPos(1);
}
ctx.getDataReader().incrementPos(1);
}
/**
@@ -75,7 +73,7 @@ class AssocOneHelpRefInherit extends AssocOneHelp {
@Override
void appendSelect(DbSqlContext ctx, boolean subQuery) {
if (!subQuery && inherit.hasChildren()) {
if (!subQuery) {
// add discriminator column
String relativePrefix = ctx.getRelativePrefix(property.getName());
String tableAlias = ctx.getTableAlias(relativePrefix);
@@ -10,7 +10,7 @@ public interface DbSqlContext {
/**
* Add a join to the sql query.
*/
void addJoin(String type, String table, TableJoinColumn[] cols, String a1, String a2, String inheritance);
void addJoin(String type, String table, TableJoinColumn[] cols, String a1, String a2);
/**
* Push the current table alias onto the stack.
@@ -219,9 +219,6 @@ public class InheritInfo {
* Return the associated InheritInfo for this DB row read.
*/
public InheritInfo readType(DbReadContext ctx) throws SQLException {
if (!hasChildren()) {
return this;
}
return readType(ctx.getDataReader().getString());
}
@@ -154,11 +154,8 @@ public final class TableJoin {
public SqlJoinType addJoin(SqlJoinType joinType, String a1, String a2, DbSqlContext ctx) {
String inheritance = inheritInfo != null ? inheritInfo.getWhere() : null;
String joinLiteral = joinType.getLiteral(type);
ctx.addJoin(joinLiteral, table, columns(), a1, a2, inheritance);
ctx.addJoin(joinLiteral, table, columns(), a1, a2);
return joinType.autoToOuter(type);
}
@@ -112,7 +112,7 @@ class DefaultDbSqlContext implements DbSqlContext {
}
@Override
public void addJoin(String type, String table, TableJoinColumn[] cols, String a1, String a2, String inheritance) {
public void addJoin(String type, String table, TableJoinColumn[] cols, String a1, String a2) {
if (tableJoins == null) {
tableJoins = new HashSet<>();
@@ -166,11 +166,6 @@ class DefaultDbSqlContext implements DbSqlContext {
}
}
// add on any inheritance where clause
if (inheritance != null && !inheritance.isEmpty()) {
sb.append(" and ").append(a2).append(".").append(inheritance);
}
if (addAsOfOnClause) {
sb.append(" and ").append(historySupport.getAsOfPredicate(a2));
}
@@ -227,7 +227,7 @@ class SqlTreeNodeBean implements SqlTreeNode {
@Override
void initBeanType() throws SQLException {
InheritInfo localInfo = inheritInfo.readType(ctx);
InheritInfo localInfo = readId ? inheritInfo.readType(ctx) : desc.getInheritInfo();
if (localInfo == null) {
// the bean must be null
localIdBinder = idBinder;
@@ -539,7 +539,7 @@ class SqlTreeNodeBean implements SqlTreeNode {
}
if (readId) {
if (!subQuery && inheritInfo != null && inheritInfo.hasChildren()) {
if (!subQuery && inheritInfo != null) {
ctx.appendColumn(inheritInfo.getDiscriminatorColumn());
}
appendSelectId(ctx, idBinder.getBeanProperty());
@@ -21,8 +21,7 @@ public class TestInheritanceBothSides extends BaseTestCase {
final Query<SourceBase> query = DB.find(SourceBase.class).orderBy("pos");
query.findList();
//assertThat(sqlOf(query)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t0.target_id, t0.target_id from source_base t0 order by t0.pos");
assertThat(sqlOf(query)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t0.target_id, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id and t1.dtype = 'Target1' order by t0.pos");
assertThat(sqlOf(query)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t0.target_id, t1.dtype, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id order by t0.pos");
}
@Test
@@ -31,8 +30,7 @@ public class TestInheritanceBothSides extends BaseTestCase {
final Query<SourceA> query = DB.find(SourceA.class).orderBy("pos");
query.findList();
//assertThat(sqlOf(query)).contains("select t0.id, t0.name, t0.pos, t0.target_id from source_base t0 where t0.dtype = 'SourceA' order by t0.pos");
assertThat(sqlOf(query)).contains("select t0.id, t0.name, t0.pos, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id and t1.dtype = 'Target1' where t0.dtype = 'SourceA' order by t0.pos");
assertThat(sqlOf(query)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id where t0.dtype = 'SourceA' order by t0.pos");
}
@Test
@@ -41,7 +39,7 @@ public class TestInheritanceBothSides extends BaseTestCase {
final Query<SourceA> query = DB.find(SourceA.class).fetch("target", "name").orderBy("pos");
query.findList();
assertThat(sqlOf(query)).contains("select t0.id, t0.name, t0.pos, t1.id, t1.name from source_base t0 left join target_base t1 on t1.id = t0.target_id and t1.dtype = 'Target1' where t0.dtype = 'SourceA' order by t0.pos");
assertThat(sqlOf(query)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t1.id, t1.name from source_base t0 left join target_base t1 on t1.id = t0.target_id where t0.dtype = 'SourceA' order by t0.pos");
}
@Test
@@ -91,7 +89,7 @@ public class TestInheritanceBothSides extends BaseTestCase {
final List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("select t0.id, t0.name, t0.pos, t1.id, t1.name from source_base t0 left join target_base t1 on t1.id = t0.target_id and t1.dtype = 'Target1' where t0.dtype = 'SourceA' order by t0.pos");
assertThat(sql.get(0)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t1.id, t1.name from source_base t0 left join target_base t1 on t1.id = t0.target_id where t0.dtype = 'SourceA' order by t0.pos");
}
/**
@@ -119,9 +117,9 @@ public class TestInheritanceBothSides extends BaseTestCase {
assertThat(sql).hasSize(3);
//assertThat(sql.get(0)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t0.target_id, t0.target_id from source_base t0 order by t0.pos");
assertThat(sql.get(0)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t0.target_id, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id and t1.dtype = 'Target1' order by t0.pos");
assertThat(sql.get(1)).contains("select t0.id, t0.name from target_base t0 where t0.dtype = 'Target1' and t0.id ");
assertThat(sql.get(2)).contains("select t0.id, t0.name from target_base t0 where t0.dtype = 'Target2' and t0.id = ?");
assertThat(sql.get(0)).contains("select t0.dtype, t0.id, t0.name, t0.pos, t1.dtype, t0.target_id, t1.dtype, t0.target_id from source_base t0 left join target_base t1 on t1.id = t0.target_id order by t0.pos");
assertThat(sql.get(1)).contains("select t0.dtype, t0.id, t0.name from target_base t0 where t0.dtype = 'Target1' and t0.id ");
assertThat(sql.get(2)).contains("select t0.dtype, t0.id, t0.name from target_base t0 where t0.dtype = 'Target2' and t0.id = ?");
}
private void assertSourceBaseEqual(SourceBase foundA, SourceBase sourceA) {
@@ -130,6 +128,10 @@ public class TestInheritanceBothSides extends BaseTestCase {
}
private void assertSourceAEqual(SourceA found, SourceA source) {
final Target1 target = found.getTarget();
final String name = target.getName();
assertThat(name).isEqualTo(source.getTarget().getName());
assertThat(found.getTarget().getId()).isEqualTo(source.getTarget().getId());
assertThat(found.getTarget().getName()).isEqualTo(source.getTarget().getName());
assertThat(found.getTarget().getClass()).isEqualTo(source.getTarget().getClass());
@@ -1,6 +1,7 @@
package org.tests.model.carwheeltruck;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@@ -9,7 +10,6 @@ import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;
import java.util.List;
@Entity
@@ -27,12 +27,11 @@ public class TestMTOInheritNoDiscriminator extends BaseTestCase {
assertThat(found.getItems()).hasSize(2);
assertThat(found.getItems()).extracting(TTruckHolderItem::getFoo).contains("a","b");
final List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(1);
if (isH2() || isPostgres()) {
assertThat(sql.get(0)).contains(" and t2.type = 'truck' ");
assertThat(sql.get(0)).contains("select t0.id, t0.name, t0.version, t2.type, t0.truck_plate_no, t0.basic_id, t1.id, t1.some_uid, t1.foo, t1.owner_id from ttruck_holder t0 join tcar t2 on t2.plate_no = t0.truck_plate_no left join ttruck_holder_item t1 on t1.owner_id = t0.id where t0.id = ? order by t0.id");
}
}
@@ -15,7 +15,6 @@ public class TestMediaInheritanceJoinToMany extends BaseTestCase {
@Test
public void test() {
String name = "nopic" + new Random().nextInt();
MProfile profileWithNoPic = new MProfile();
@@ -29,17 +28,9 @@ public class TestMediaInheritanceJoinToMany extends BaseTestCase {
MProfile profile = query.findOne();
Assert.assertNotNull(profile);
// select t0.id c0, t0.name c1, t1.type c2, t1.id c3, t1.url c4, t1.note c5
// from profile t0
// left join media t1 on t1.id = t0.picture_id and t1.type = 'Picture'
// where t0.name = ? ; --bind(nopic)
// specifically t1.type = 'Picture' ... on on the join and not in the where
String generatedSql = query.getGeneratedSql();
assertThat(generatedSql).contains("from mprofile t0 left join mmedia t1 on t1.id = t0.picture_id and t1.type = 'Picture' ");
assertThat(generatedSql).contains("select t0.id, t0.name, t1.type, t1.id, t1.url, t1.note from mprofile t0 left join mmedia t1 on t1.id = t0.picture_id where t0.name = ?");
assertThat(generatedSql).contains("where t0.name = ?");
}
}