mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-13 22:54:51 +00:00
HHH-13742 - Preliminary fix for HHH-13742
This commit is contained in:
parent
8089d8c575
commit
325239353e
@ -8,6 +8,7 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -31,6 +32,7 @@
|
||||
import org.hibernate.hql.internal.ast.tree.ImpliedFromElement;
|
||||
import org.hibernate.hql.internal.ast.tree.ParameterContainer;
|
||||
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.internal.CoreLogging;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
@ -154,9 +156,7 @@ private Set<String> findQueryReferencedTables(QueryNode query) {
|
||||
if ( entityPersister != null && entityPersister instanceof AbstractEntityPersister ) {
|
||||
AbstractEntityPersister aep = (AbstractEntityPersister) entityPersister;
|
||||
String[] tables = aep.getTableNames();
|
||||
for ( String table : tables ) {
|
||||
result.add( table );
|
||||
}
|
||||
Collections.addAll(result, tables);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -171,10 +171,25 @@ private void collectReferencedTables(ASTIterator iterator, Set<String> result) {
|
||||
FromReferenceNode fromReferenceNode = (FromReferenceNode) node;
|
||||
String[] tables = fromReferenceNode.getReferencedTables();
|
||||
if ( tables != null ) {
|
||||
for ( String table : tables ) {
|
||||
result.add( table );
|
||||
Collections.addAll(result, tables);
|
||||
}
|
||||
}
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@
|
||||
*/
|
||||
package org.hibernate.query.hhh13670;
|
||||
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Before;
|
||||
@ -112,11 +111,39 @@ public void testSubTypePropertyReferencedFromEntityJoinInSyntheticSubquery() {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration configuration) {
|
||||
super.afterConfigurationBuilt( configuration );
|
||||
// Uncomment to fix tests
|
||||
// configuration.setProperty( AvailableSettings.OMIT_JOIN_OF_SUPERCLASS_TABLES, "false" );
|
||||
@Test
|
||||
public void testSubTypePropertyReferencedFromEntityJoinInSyntheticSubquery2() {
|
||||
doInJPA(this::sessionFactory, em -> {
|
||||
List<Tuple> resultList = em.createQuery(
|
||||
"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
|
||||
|
Loading…
x
Reference in New Issue
Block a user