mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +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:
@@ -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