HHH-13742 - Preliminary fix for HHH-13742

This commit is contained in:
Jan-Willem Gmelig Meyling 2019-11-21 16:06:23 +01:00
parent 8089d8c575
commit 325239353e
2 changed files with 53 additions and 11 deletions

View File

@ -8,6 +8,7 @@ package org.hibernate.hql.internal.ast.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -31,6 +32,7 @@ import org.hibernate.hql.internal.ast.tree.FromReferenceNode;
import org.hibernate.hql.internal.ast.tree.ImpliedFromElement; import org.hibernate.hql.internal.ast.tree.ImpliedFromElement;
import org.hibernate.hql.internal.ast.tree.ParameterContainer; import org.hibernate.hql.internal.ast.tree.ParameterContainer;
import org.hibernate.hql.internal.ast.tree.QueryNode; import org.hibernate.hql.internal.ast.tree.QueryNode;
import org.hibernate.hql.internal.ast.tree.SqlFragment;
import org.hibernate.hql.internal.classic.ParserHelper; import org.hibernate.hql.internal.classic.ParserHelper;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
@ -154,9 +156,7 @@ public class JoinProcessor implements SqlTokenTypes {
if ( entityPersister != null && entityPersister instanceof AbstractEntityPersister ) { if ( entityPersister != null && entityPersister instanceof AbstractEntityPersister ) {
AbstractEntityPersister aep = (AbstractEntityPersister) entityPersister; AbstractEntityPersister aep = (AbstractEntityPersister) entityPersister;
String[] tables = aep.getTableNames(); String[] tables = aep.getTableNames();
for ( String table : tables ) { Collections.addAll(result, tables);
result.add( table );
}
} }
} }
} }
@ -171,10 +171,25 @@ public class JoinProcessor implements SqlTokenTypes {
FromReferenceNode fromReferenceNode = (FromReferenceNode) node; FromReferenceNode fromReferenceNode = (FromReferenceNode) node;
String[] tables = fromReferenceNode.getReferencedTables(); String[] tables = fromReferenceNode.getReferencedTables();
if ( tables != null ) { if ( tables != null ) {
for ( String table : tables ) { Collections.addAll(result, tables);
result.add( table );
} }
} }
else if (node instanceof SqlFragment) {
SqlFragment sqlFragment = (SqlFragment) node;
FromElement fromElement = sqlFragment.getFromElement();
if (fromElement != null) {
// For joins, we want to add the table where the association key is mapped as well as that could be a supertype that we need to join
String role = fromElement.getRole();
if ( role != null ) {
result.add( fromElement.getOrigin().getPropertyTableName(role.substring(role.lastIndexOf('.') + 1)) );
}
AST withClauseAst = fromElement.getWithClauseAst();
if ( withClauseAst != null ) {
collectReferencedTables( new ASTIterator( withClauseAst ), result );
}
}
} }
} }
} }

View File

@ -6,7 +6,6 @@
*/ */
package org.hibernate.query.hhh13670; package org.hibernate.query.hhh13670;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before; import org.junit.Before;
@ -112,11 +111,39 @@ public class HHH13670Test extends BaseCoreFunctionalTestCase {
}); });
} }
@Override @Test
protected void configure(Configuration configuration) { public void testSubTypePropertyReferencedFromEntityJoinInSyntheticSubquery2() {
super.afterConfigurationBuilt( configuration ); doInJPA(this::sessionFactory, em -> {
// Uncomment to fix tests List<Tuple> resultList = em.createQuery(
// configuration.setProperty( AvailableSettings.OMIT_JOIN_OF_SUPERCLASS_TABLES, "false" ); "SELECT subB_0.id, subA_0.id, subB_0.id, subA_0.id FROM SubB subB_0 INNER JOIN SubA subA_0 ON 1=1 WHERE (EXISTS (SELECT 1 FROM Super s WHERE subA_0.id = s.parent.id)) ORDER BY subB_0.id ASC, subA_0.id ASC", Tuple.class)
.getResultList();
assertEquals(4, resultList.size());
});
}
@Test
public void testSubTypePropertyReferencedFromWhereClause() {
doInJPA(this::sessionFactory, em -> {
List<Tuple> resultList = em.createQuery("SELECT subB_0.id FROM SubB subB_0 WHERE subB_0.parent.id IS NOT NULL", Tuple.class)
.getResultList();
});
}
@Test
public void testSubTypePropertyReferencedFromGroupByClause() {
doInJPA(this::sessionFactory, em -> {
List<Tuple> resultList = em.createQuery("SELECT subB_0.id FROM SubB subB_0 GROUP BY subB_0.id , subB_0.parent.id", Tuple.class)
.getResultList();
});
}
@Test
public void testSubTypePropertyReferencedFromOrderByClause() {
doInJPA(this::sessionFactory, em -> {
List<Tuple> resultList = em.createQuery("SELECT subB_0.id FROM SubB subB_0 ORDER BY subB_0.id , subB_0.parent.id", Tuple.class)
.getResultList();
});
} }
@Override @Override