#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,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 = ?");
}
}