mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-28 15:27:56 +00:00
#715 - isNull() expression on @OneToMany or @ManyToMany produces incorrect SQL - should translate this to be same as isEmpty() and result in exists subquery
This commit is contained in:
1 parent
0510304c0f
commit
a20822a9e1
12 files changed
+171
-55
No files matched your search
+2
-1
@@ -6,6 +6,7 @@ import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.event.BeanQueryRequest;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
|
||||
import com.avaje.ebeaninternal.api.ManyWhereJoins;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
@@ -55,8 +56,8 @@ public class DefaultExampleExpressionTest extends BaseTestCase {
|
||||
|
||||
SpiQuery<Customer> query = (SpiQuery<Customer>)spiEbeanServer().find(Customer.class);
|
||||
BeanQueryRequest<?> request = create(query);
|
||||
expr.containsMany(customerBeanDescriptor(), new ManyWhereJoins());
|
||||
expr.prepareExpression(request);
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
import com.avaje.ebeaninternal.api.ManyWhereJoins;
|
||||
import com.avaje.ebeaninternal.api.SpiExpression;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -8,7 +10,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class NullExpressionTest extends BaseExpressionTest {
|
||||
|
||||
NullExpression nullExp(String propertyName, boolean notNull) {
|
||||
return new NullExpression(propertyName, notNull);
|
||||
NullExpression expr = new NullExpression(propertyName, notNull);
|
||||
expr.containsMany(getBeanDescriptor(Order.class), new ManyWhereJoins());
|
||||
return expr;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -6,53 +6,101 @@ import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import com.avaje.tests.model.m2m.Role;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestQueryIsNull extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void queryShouldContainIsNullOnColumn() {
|
||||
ResetBasicData.reset();
|
||||
@Test
|
||||
public void queryShouldContainIsNullOnColumn() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class).where().isNull("customerName").query();
|
||||
query.findList();
|
||||
Query<Order> query = Ebean.find(Order.class).where().isNull("customerName").query();
|
||||
query.findList();
|
||||
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("name is null"));
|
||||
}
|
||||
assertTrue(query.getGeneratedSql().contains("name is null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryShouldLeftJoinForOneToManyInDisjunction() {
|
||||
ResetBasicData.reset();
|
||||
@Test
|
||||
public void isNotNull_when_OneToMany_expect_existsSubquery() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class).where().disjunction().isNull("details").endJunction().query();
|
||||
query.findList();
|
||||
Query<Order> query = Ebean.find(Order.class).where().isNotNull("details").query();
|
||||
query.findList();
|
||||
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("left outer join"));
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("is null"));
|
||||
}
|
||||
assertTrue(query.getGeneratedSql().contains(" where exists (select 1 from o_order_detail where order_id = t0.id)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryShouldLeftJoinForOneToManyRelation() {
|
||||
ResetBasicData.reset();
|
||||
@Test
|
||||
public void isNotEmpty_when_OneToMany_expect_existsSubquery() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class).where().isNull("details").query();
|
||||
query.findList();
|
||||
Query<Order> query = Ebean.find(Order.class).where().isNotEmpty("details").query();
|
||||
query.findList();
|
||||
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("left outer join"));
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("is null"));
|
||||
assertTrue(query.getGeneratedSql().contains(" where exists (select 1 from o_order_detail where order_id = t0.id)"));
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void isNull_when_OneToMany_expect_notExistsSubquery() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
@Test
|
||||
public void queryShouldLeftJoinForManyToManyRelation() {
|
||||
ResetBasicData.reset();
|
||||
Query<Order> query = Ebean.find(Order.class).where().isNull("details").query();
|
||||
query.findList();
|
||||
|
||||
Query<Role> query = Ebean.find(Role.class).where().isNull("permissions").query();
|
||||
query.findList();
|
||||
assertTrue(query.getGeneratedSql().contains(" where not exists (select 1 from o_order_detail where order_id = t0.id)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty_when_OneToMany_expect_notExistsSubquery() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class).where().isEmpty("details").query();
|
||||
query.findList();
|
||||
|
||||
assertTrue(query.getGeneratedSql().contains(" where not exists (select 1 from o_order_detail where order_id = t0.id)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty_when_ManyToMany_expect_notExistsSubqueryAndNoJoin() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Role> query = Ebean.find(Role.class).where().isEmpty("permissions").query();
|
||||
query.findList();
|
||||
|
||||
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where not exists (select 1 from mt_role_permission where mt_role_id = t0.id)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNull_when_ManyToMany_expect_notExistsSubqueryAndNoJoin() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Role> query = Ebean.find(Role.class).where().isNull("permissions").query();
|
||||
query.findList();
|
||||
|
||||
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where not exists (select 1 from mt_role_permission where mt_role_id = t0.id)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmpty_when_ManyToMany_expect_existsSubqueryAndNoJoin() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Role> query = Ebean.find(Role.class).where().isNotEmpty("permissions").query();
|
||||
query.findList();
|
||||
|
||||
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where exists (select 1 from mt_role_permission where mt_role_id = t0.id)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotNull_when_ManyToMany_expect_existsSubqueryAndNoJoin() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Role> query = Ebean.find(Role.class).where().isNotNull("permissions").query();
|
||||
query.findList();
|
||||
|
||||
assertTrue(query.getGeneratedSql().contains("from mt_role t0 where exists (select 1 from mt_role_permission where mt_role_id = t0.id)"));
|
||||
}
|
||||
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("left outer join"));
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("is null"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user