I use hibernate jpa which verison is 4.3.4.Final. When I use named query with 'left join fetch' from a @ElementCollection set fetch an enetity which is referenced by this set,an QueryException occured.
My entity models with annotation mapping see below:
1. User entity
Code:
@Entity
@Table(name = "T_SEC_User")
@SecondaryTable(name = "T_SEC_UserAccount", pkJoinColumns = { @PrimaryKeyJoinColumn(name = "userId") })
public class User {
private static final long serialVersionUID = 1346363505539059955L;
@Column(name = "userSecurityStrategyId")
private String userSecurityStrategyId;
@Column(name = "withPersonId")
private String withPersonId;
@Column(name = "masterPartyId")
private String masterPartyId;
[b]@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "T_SEC_User_Party_Role",joinColumns={@JoinColumn(name = "userId")})
private Set<UserPartyRole> partyRole = new HashSet<UserPartyRole>();[/b]
// getter & setter ...
}
2.UserPartyRole embedded value object
Code:
@Embeddable
public class UserPartyRole {
private static final long serialVersionUID = 1502825051129774164L;
@Column(name = "partyId", length = 32)
private String partyId;
[b]@ManyToOne(cascade = CascadeType.DETACH,fetch=FetchType.LAZY)
@JoinColumn(name = "roleId")
private Role role;[/b]
//getter & setter...
}
3.Role entity
Code:
@Entity
@Table(name = "T_SEC_Role")
public class Role {
private static final long serialVersionUID = 1L;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "T_SEC_RolePermission", joinColumns = { @JoinColumn(name = "roleId") })
private Set<RolePermission> permissions = new HashSet<RolePermission>();
@Column(name = "systemDefault")
private boolean systemDefault = false;
//getter & setter....
}
4.RolePermission embedded value object
Code:
@Embeddable
public class RolePermission{
private static final long serialVersionUID = 1616865746541549333L;
@Column(name = "permissionKey", length = 512)
private String permissionKey;
@Column(name = "permissionStrategies")
private String permissionStrategies;
//getter & setter...
}
The named query is "
SELECT u FROM User u left join fetch u.partyRole pr left join fetch pr.role WHERE u.id = :id".
When I use it,an exception occured.The exception see below:
Quote:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null.role,tableName=T_SEC_Role,tableAlias=role2_,origin=T_SEC_User_Party_Role partyrole1_,columns={partyrole1_.roleId ,className=Role}}] [SELECT distinct u FROM User u left join fetch u.partyRole upr left join fetch upr.role WHERE u.id = :id and u.lifecycleStages!='DISABLE']
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)...
Caused by: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null.role,tableName=T_SEC_Role,tableAlias=role2_,origin=T_SEC_User_Party_Role partyrole1_,columns={partyrole1_.roleId ,className=Role}}]
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:227)
at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:923)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:691)...
Who can help me? Thanks a lot.