我正在构建JPA Criteria查询如下所述。基本上UserInfo是我的父实体,UserSecurityRole是我的子实体,并且具有从父到子的一对多映射。在生成的sql查询中我注意到,它包含主键和order by的where子句不包含在查询中。 基本上我不应该有where子句但我得到它。你能建议正确的方法。下面是代码片段。
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<UserInfo> criteriaQuery = criteriaBuilder.createQuery(UserInfo.class);
Root<UserSecurityRole> from = criteriaQuery.from(UserSecurityRole.class);
Join<UserSecurityRole,UserInfo> users = from.join("users");
criteriaQuery.select(users);
criteriaQuery.orderBy(criteriaBuilder.asc(users.get("userId")));
TypedQuery<UserInfo> typedQuery = em.createQuery(criteriaQuery);
List<UserInfo> usersList = typedQuery.getResultList();
return usersList;
生成的查询:
select userinfo0_.USER_ID as USER_ID1_14_0_, userinfo0_.EMAIL as EMAIL13_14_0_, securityro2_.LAST_MODIFIED_DATE as LAST_MODIFIED_DATE7_10_3_, securityro2_.MDATE_TZ_CODE as MDATE_TZ_CODE8_10_3_, securityro2_.name as name9_10_3_
from USER userinfo0_
left outer join bipkgownr.SECURITY_ROLE usersecuri1_ on userinfo0_.USER_ID=usersecuri1_.USER_ID
left outer join SECURITY_ROLE securityro2_ on usersecuri1_.SR_RECORD_ID=securityro2_.SR_RECORD_ID
where userinfo0_.USER_ID=?