HHH-9329: Added testcase that demonstrates issue with join table index column usage

(cherry picked from commit 5d6678a414)
This commit is contained in:
Christian Beikov 2016-09-26 11:16:42 +02:00 committed by Gail Badner
parent a9923d2be7
commit 1fdaf71bd3
1 changed files with 28 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -164,6 +165,27 @@ public class WithClauseTest extends BaseCoreFunctionalTestCase {
data.cleanup(); data.cleanup();
} }
@Test
@TestForIssue(jiraKey = "HHH-9329")
public void testWithClauseAsSubqueryWithKey() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction txn = s.beginTransaction();
// Since family has a join table, we will first left join all family members and then do the WITH clause on the target entity table join
// Normally this produces 2 results which is wrong and can only be circumvented by converting the join table and target entity table join to a subquery
List list = s.createQuery( "from Human h left join h.family as f with key(f) like 'son1' where h.description = 'father'" )
.list();
assertEquals( "subquery rewriting of join table did not take effect", 1, list.size() );
txn.commit();
s.close();
data.cleanup();
}
private class TestData { private class TestData {
public void prepare() { public void prepare() {
Session session = openSession(); Session session = openSession();
@ -214,6 +236,10 @@ public class WithClauseTest extends BaseCoreFunctionalTestCase {
session.save( friend ); session.save( friend );
session.save( friend2 ); session.save( friend2 );
father.setFamily( new HashMap() );
father.getFamily().put( "son1", child1 );
father.getFamily().put( "son2", child2 );
txn.commit(); txn.commit();
session.close(); session.close();
} }
@ -223,7 +249,9 @@ public class WithClauseTest extends BaseCoreFunctionalTestCase {
Transaction txn = session.beginTransaction(); Transaction txn = session.beginTransaction();
Human father = (Human) session.createQuery( "from Human where description = 'father'" ).uniqueResult(); Human father = (Human) session.createQuery( "from Human where description = 'father'" ).uniqueResult();
father.getFriends().clear(); father.getFriends().clear();
father.getFamily().clear();
session.flush(); session.flush();
session.delete( session.createQuery( "from Human where description = 'friend2'" ).uniqueResult() );
session.delete( session.createQuery( "from Human where description = 'friend'" ).uniqueResult() ); session.delete( session.createQuery( "from Human where description = 'friend'" ).uniqueResult() );
session.delete( session.createQuery( "from Human where description = 'child1'" ).uniqueResult() ); session.delete( session.createQuery( "from Human where description = 'child1'" ).uniqueResult() );
session.delete( session.createQuery( "from Human where description = 'child2'" ).uniqueResult() ); session.delete( session.createQuery( "from Human where description = 'child2'" ).uniqueResult() );