HHH-14116 Exception when fetch joining a non-collection when selecting non-query-root

This commit is contained in:
Nathan Xu 2020-07-25 13:33:33 -04:00 committed by Christian Beikov
parent 3e07791683
commit 636ef89fcf
2 changed files with 87 additions and 3 deletions

View File

@ -208,9 +208,7 @@ public class SelectClause extends SelectExpressionList {
else {
origin = fromElement.getRealOrigin();
}
if ( !fromElementsForLoad.contains( origin )
// work around that fetch joins of element collections where their parent instead of the root is selected
&& ( !fromElement.isCollectionJoin() || !fromElementsForLoad.contains( fromElement.getFetchOrigin() ) ) ) {
if ( !fromElementsForLoad.contains( origin ) && !fromElementsForLoad.contains( fromElement.getFetchOrigin() ) ) {
throw new QueryException(
"query specified join fetching, but the owner " +
"of the fetched association was not present in the select list " +

View File

@ -0,0 +1,86 @@
package org.hibernate.query.hhh14116;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
/**
* @author Christian Beikov
* @author Nathan Xu
*/
@TestForIssue( jiraKey = "HHH-14116" )
public class HHH14116Test extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { HHH14116Test.User.class, HHH14116Test.Group.class };
}
@Test
public void testNoExceptionThrown() {
doInJPA( this::sessionFactory, em -> {
em.createQuery(
"SELECT g FROM User u JOIN u.groups g JOIN FETCH g.permissions JOIN FETCH g.tenant where u.id = ?1", Group.class )
.setParameter(1, 1L )
.getResultList();
}
);
}
@Entity(name = "User")
@Table(name = "usr_tbl")
public static class User {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToMany
private Set<Group> groups;
@Enumerated(value = EnumType.STRING)
@ElementCollection
private Set<Permission> permissions;
}
@Entity(name = "Group")
@Table(name = "grp_tbl")
public static class Group {
@Id
@GeneratedValue
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private User tenant;
@Enumerated(value = EnumType.STRING)
@ElementCollection
private Set<Permission> permissions;
}
public enum Permission {
READ,
WRITE,
EXECUTE;
}
}