JPA 2没有明确的选择,也没有确定隐含的一种感冒 [英] JPA 2 No explicit selection and an implicit one cold not be determined
本文介绍了JPA 2没有明确的选择,也没有确定隐含的一种感冒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为某个日期之后在其中创建用户的文件夹获取所有用户。用户和文件夹之间的关系位于单独的表中。
I am trying to fetch all users for a folder where the user was created after a certain date. the relationship between the user and the folder lives in a seperate table.
这是我提出的查询,但它可以解决异常情况
This is the query I came up with but it thorws the exception
没有明确的选择和不确定的隐患
No explicit selection and an implicit one cold not be determined
代码
@Override
public List<RetailPostUserTbl> getNewUsersForSiteSince( Date date, Integer siteId )
{
List<RetailPostUserTbl> toReturn = new ArrayList<RetailPostUserTbl>();
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
Class<RpUserFolderMapTbl> userFolderPC = userFolderMapDAO.getPersistentClass();
CriteriaQuery<RpUserFolderMapTbl> mapQuery = cb.createQuery( userFolderPC );
Root<RpUserFolderMapTbl> root = mapQuery.from( userFolderPC );
Path<Integer> folderIdPath = root.get( RpUserFolderMapTbl_.folder ).get( FolderTbl_.folderId );
Predicate folderCondition = cb.equal( folderIdPath, siteId );
Subquery<RetailPostUserTbl> rpSubQ = mapQuery.subquery( persistentClass );
Root<RetailPostUserTbl> subQRoot = rpSubQ.from( persistentClass );
Path<UserTbl> userPath = subQRoot.get( RetailPostUserTbl_.user );
Path<Date> userCreatedPath = userPath.get( UserTbl_.userCreateDate );
Predicate userCreateDateCondition = cb.greaterThanOrEqualTo( userCreatedPath, date );
rpSubQ.where( userCreateDateCondition );
mapQuery.where( cb.and( folderCondition, cb.exists( rpSubQ ) ) );
TypedQuery<RpUserFolderMapTbl> query = em.createQuery( mapQuery );
List<RpUserFolderMapTbl> results = query.getResultList();
for ( RpUserFolderMapTbl result : results )
{
RetailPostUserTbl rpuser = result.getUser().getRetailPostUser();
toReturn.add( rpuser );
}
return toReturn;
}
有人知道为什么这行不通吗?
Anyone know why this is not working?
推荐答案
还应该为子查询显式设置选择。
You should set explicitly selection also for "subqueries".
rpSubQ.select(subQRoot);
这篇关于JPA 2没有明确的选择,也没有确定隐含的一种感冒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文