#2580 - Fix for Wrong join in query with @Inheritance

Effectively delays an extra join added via BeanPropertyAssocOne with inheritance, such that this only occurs if a SqlTreeNode child doesn't add the join.
This commit is contained in:
Rob Bygrave committed 2022-03-14 15:37:12 +13:00
1 parent c902caed24
commit 267b1b7303
14 files changed
+130 -85

No files matched your search

@@ -9,10 +9,10 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestMTOInheritNoDiscriminator extends BaseTestCase {
class TestMTOInheritNoDiscriminator extends BaseTestCase {
@Test
public void test() {
void test() {
final TTruckHolder holder = setup();
@@ -31,7 +31,7 @@ public class TestMTOInheritNoDiscriminator extends BaseTestCase {
assertThat(sql).hasSize(1);
if (isH2() || isPostgresCompatible()) {
assertSql(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");
assertSql(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 left join ttruck_holder_item t1 on t1.owner_id = t0.id join tcar t2 on t2.plate_no = t0.truck_plate_no where t0.id = ? order by t0.id");
}
}
@@ -5,6 +5,6 @@ import javax.persistence.Entity;
@DiscriminatorValue("A")
@Entity
public class AccountAccess extends Access {
public class AccountAccess extends HAccess {
}
}
@@ -5,6 +5,6 @@ import javax.persistence.Entity;
@DiscriminatorValue("B")
@Entity
public class BankAccount extends Account {
}
public class BankAccount extends HAccount {
}
@@ -5,6 +5,6 @@ import javax.persistence.Entity;
@DiscriminatorValue("C")
@Entity
public class CustomerAccess extends Access {
public class CustomerAccess extends HAccess {
}
}
@@ -8,13 +8,11 @@ import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.tests.model.basic.Customer;
import io.ebean.annotation.DbForeignKey;
@Inheritance
@Entity
public abstract class Access {
public abstract class HAccess {
@Id
UUID id;
@@ -22,17 +20,17 @@ public abstract class Access {
@ManyToOne
@DbForeignKey(noConstraint = true)
@JoinColumn(name = "accessor_id")
ACustomer accessor;
HCustomer accessor;
@ManyToOne
@DbForeignKey(noConstraint = true)
@JoinColumn(name = "principal_id")
ACustomer principal;
HCustomer principal;
@ManyToOne
@DbForeignKey(noConstraint = true)
@JoinColumn(name = "access_account_number")
Account account;
HAccount account;
public UUID getId() {
return id;
@@ -42,27 +40,27 @@ public abstract class Access {
this.id = id;
}
public ACustomer getAccessor() {
public HCustomer getAccessor() {
return accessor;
}
public void setAccessor(ACustomer accessor) {
public void setAccessor(HCustomer accessor) {
this.accessor = accessor;
}
public ACustomer getPrincipal() {
public HCustomer getPrincipal() {
return principal;
}
public void setPrincipal(ACustomer principal) {
public void setPrincipal(HCustomer principal) {
this.principal = principal;
}
public Account getAccount() {
public HAccount getAccount() {
return account;
}
public void setAccount(Account account) {
public void setAccount(HAccount account) {
this.account = account;
setPrincipal(account.getOwner());
}
@@ -2,18 +2,16 @@ package org.tests.model.join;
import javax.persistence.*;
import org.tests.model.basic.Customer;
@Entity
@Inheritance
public abstract class Account {
public abstract class HAccount {
@Id
String accountNumber;
@ManyToOne
@JoinColumn(name = "owner_id")//, referencedColumnName = "id")
ACustomer owner;
@JoinColumn(name = "owner_id")
HCustomer owner;
public String getAccountNumber() {
return accountNumber;
@@ -23,11 +21,11 @@ public abstract class Account {
this.accountNumber = accountNumber;
}
public ACustomer getOwner() {
public HCustomer getOwner() {
return owner;
}
public void setOwner(ACustomer owner) {
public void setOwner(HCustomer owner) {
this.owner = owner;
}
@@ -4,7 +4,7 @@ import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class ACustomer {
public class HCustomer {
@Id
final String cid;
@@ -13,7 +13,7 @@ public class ACustomer {
String status = "A";
public ACustomer(String cid, String name) {
public HCustomer(String cid, String name) {
this.cid = cid;
this.name = name;
}
@@ -1,80 +1,69 @@
package org.tests.query;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.Customer.Status;
import org.tests.model.basic.ResetBasicData;
import org.tests.model.join.Access;
import org.tests.model.join.Account;
import org.tests.model.join.AccountAccess;
import org.tests.model.join.BankAccount;
import org.tests.model.join.CustomerAccess;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.Query;
import org.junit.jupiter.api.Test;
import org.tests.model.join.*;
public class TestQueryMultiJoinFetchPath extends BaseTestCase {
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class TestQueryMultiJoinFetchPath extends BaseTestCase {
@Test
public void test() {
void test() {
ResetBasicData.reset();
Customer c1 = new Customer();
c1.setName("c1");
c1.setStatus(Status.ACTIVE);
HCustomer c1 = new HCustomer("c1", "c1");
DB.save(c1);
Customer c2 = new Customer();
c2.setName("c2");
c2.setStatus(Status.ACTIVE);
HCustomer c2 = new HCustomer("c2", "c2");
DB.save(c2);
Customer c3 = new Customer();
c3.setName("c3");
c3.setStatus(Status.ACTIVE);
HCustomer c3 = new HCustomer("c3", "c3");
DB.save(c3);
Account a1 = new BankAccount();
HAccount a1 = new BankAccount();
a1.setAccountNumber("a1");
a1.setOwner(c1);
DB.save(a1);
CustomerAccess ca = new CustomerAccess();
ca.setAccessor(c3);
ca.setPrincipal(c1);
DB.save(ca);
AccountAccess aa = new AccountAccess();
aa.setAccessor(c2);
aa.setAccount(a1);
DB.save(aa);
List<Object> ids = DB.find(Access.class)
List<Object> ids = DB.find(HAccess.class)
.where()
.eq("principal.status", Status.ACTIVE)
.eq("accessor.status", Status.ACTIVE)
.eq("principal.status", "A")
.eq("accessor.status", "A")
.findIds();
assertThat(ids).hasSize(2);
Query<Access> query = DB.find(Access.class)
Query<HAccess> query = DB.find(HAccess.class)
.fetch("account","accountNumber")
.fetch("accessor","name")
.where()
.eq("accessor.status", Status.ACTIVE)
.eq("principal.status", Status.ACTIVE)
.eq("accessor.status", "A")
.eq("principal.status", "A")
.idIn(ids)
.query();
List<Access> accesses = query.findList();
List<HAccess> accesses = query.findList();
assertThat(accesses).hasSize(2);
if (isH2()) {
assertThat(query.getGeneratedSql()).isEqualTo("select t0.dtype, t0.id, t0.accessor_id, t0.principal_id, t2.dtype, t0.access_account_number, t0.accessor_id, t2.dtype, t0.access_account_number, t1.cid, t1.name, t2.dtype, t2.account_number from haccess t0 left join hcustomer t1 on t1.cid = t0.accessor_id left join haccount t2 on t2.account_number = t0.access_account_number and t2.dtype = 'B' left join hcustomer t3 on t3.cid = t0.principal_id where t1.status = ? and t3.status = ? and t0.id in (?,?,?,?,?)");
} else {
assertThat(query.getGeneratedSql()).contains("select t0.dtype, t0.id, t0.accessor_id, t0.principal_id, t2.dtype, t0.access_account_number, t0.accessor_id, t2.dtype, t0.access_account_number, t1.cid, t1.name, t2.dtype, t2.account_number from haccess t0 left join hcustomer t1 on t1.cid = t0.accessor_id left join haccount t2 on t2.account_number = t0.access_account_number and t2.dtype = 'B' left join hcustomer t3 on t3.cid = t0.principal_id where t1.status = ? and t3.status = ? and t0.id ");
}
}
}