Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid SQL for exist queries with many to many relation. #3512

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions ebean-test/src/test/java/org/tests/m2m/TestM2MQueryAndIds.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package org.tests.m2m;


import io.ebean.DB;
import io.ebean.Query;
import io.ebean.annotation.Platform;
import io.ebean.xtest.BaseTestCase;
import io.ebean.xtest.ForPlatform;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.tests.model.m2m.Permission;
import org.tests.model.m2m.Role;
import org.tests.model.m2m.Tenant;

import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;

public class TestM2MQueryAndIds extends BaseTestCase {

@BeforeAll
public static void beforeAll() throws Exception {
Tenant tenant = new Tenant("TestTenant");
DB.save(tenant);

Permission perm = new Permission("TestPerm");
DB.save(perm);

Role role = new Role("TestRole");
role.getPermissions().add(perm);
role.setTenant(tenant);
DB.save(role);
}

@AfterAll
public static void afterAll() {
DB.find(Role.class).delete();
DB.find(Permission.class).delete();
DB.find(Tenant.class).delete();
}

@ForPlatform(Platform.H2)
@Test
public void testM2MWithId() {
Query<Permission> sq1 = DB.find(Permission.class)
.alias("sq1")
.where()
.eq("name", "TestPerm")
.raw("(sq1.id = permissions.id)")
.query();

Query<Role> query = DB.find(Role.class).select("id").where().exists(sq1).query();
List<Role> models = query.findList();
assertThat(models).hasSize(1);
assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1z_.mt_permission_id))");
// assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id left join mt_permission t1 on t1.id = t1z_.mt_permission_id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1.id))");
}

@ForPlatform(Platform.H2)
@Test
public void testM2MWithoutId() {
Query<Permission> sq1 = DB.find(Permission.class)
.alias("sq1")
.where()
.eq("name", "TestPerm")
.raw("(sq1.id = permissions)")
.query();

Query<Role> query = DB.find(Role.class).select("id").where().exists(sq1).query();
List<Role> models = query.findList();
assertThat(models).hasSize(1);
// query from testO2MWith...Id(): select t0.id from mt_role t0 where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))
assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_role t0 left join mt_role_permission t1z_ on t1z_.mt_role_id = t0.id where exists (select 1 from mt_permission sq1 where sq1.name = ? and (sq1.id = t1z_.mt_permission_id))");
}

@ForPlatform(Platform.H2)
@Test
public void testO2MWithId() {
Query<Role> sq1 = DB.find(Role.class)
.alias("sq1")
.where()
.eq("name", "TestRole")
.raw("(sq1.id = roles.id)")
.query();

Query<Tenant> query = DB.find(Tenant.class).select("id").where().exists(sq1).query();
List<Tenant> models = query.findList();
assertThat(models).hasSize(1);
assertThat(query.getGeneratedSql()).isEqualTo("select distinct t0.id from mt_tenant t0 left join mt_role t1 on t1.tenant_id = t0.id where exists (select 1 from mt_role sq1 where sq1.name = ? and (sq1.id = t1.id))");
}

@ForPlatform(Platform.H2)
@Test
public void testO2MWithoutId() {
Query<Role> sq1 = DB.find(Role.class)
.alias("sq1")
.where()
.eq("name", "TestRole")
.raw("(sq1.id = roles)")
.query();

Query<Tenant> query = DB.find(Tenant.class).select("id").where().exists(sq1).query();
List<Tenant> models = query.findList();
assertThat(models).hasSize(1);
assertThat(query.getGeneratedSql()).isEqualTo("select t0.id from mt_tenant t0 where exists (select 1 from mt_role sq1 where sq1.name = ? and (sq1.id = t0.roles_id))");
}

@ForPlatform(Platform.H2)
@Test
public void testM2OWithId() {
Query<Tenant> sq1 = DB.find(Tenant.class)
.alias("sq1")
.where()
.eq("name", "TestTenant")
.raw("(sq1.id = tenant.id)")
.query();

Query<Role> query = DB.find(Role.class).select("id").where().exists(sq1).query();
List<Role> models = query.findList();
assertThat(models).hasSize(1);
assertThat(query.getGeneratedSql()).isEqualTo("select t0.id from mt_role t0 where exists (select 1 from mt_tenant sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))");
}

@ForPlatform(Platform.H2)
@Test
public void testM2OWithoutId() {
Query<Tenant> sq1 = DB.find(Tenant.class)
.alias("sq1")
.where()
.eq("name", "TestTenant")
.raw("(sq1.id = tenant)")
.query();

Query<Role> query = DB.find(Role.class).select("id").where().exists(sq1).query();
List<Role> models = query.findList();
assertThat(models).hasSize(1);
assertThat(query.getGeneratedSql()).isEqualTo("select t0.id from mt_role t0 where exists (select 1 from mt_tenant sq1 where sq1.name = ? and (sq1.id = t0.tenant_id))");
}
}
Loading