Jpa JPQL:从多表达式选择中获取结果

Jpa JPQL:从多表达式选择中获取结果,jpa,jpql,Jpa,Jpql,以下是我的JPQL查询: SELECT p, exists( select dp from DocumentPublication dp where dp.documentVersion = p) FROM DocumentVersion p where document.id = :id 以下是获得结果的代码: Query query = getEntityManager().createNamedQuery("DocumentVersion.find

以下是我的JPQL查询:

SELECT p, 
   exists( select dp from DocumentPublication dp where dp.documentVersion = p) 
FROM 
   DocumentVersion p where document.id = :id
以下是获得结果的代码:

   Query query =   
     getEntityManager().createNamedQuery("DocumentVersion.findByDocumentId");

   query.setParameter("id", docsFilter.getProjectId());

   List<Object[]>  res;
   try
   {
       res = query.getResultList();
   }
   catch (NoResultException e)
   {
       return null;
   }
   // res only contains a list of DocumentVersion / No 'boolean'
 List<Object[]>  res;
   try
   {
       res = query.getResultList();
   }
   catch (NoResultException e)
   {
       return null;
   }

   List<DocumentVersion> documentVersions = new ArrayList<>();
   for(Object[] objects : res)
   {
      DocumentVersion documentVersion = (DocumentVersion) objects[0];
      documentVersion.setPublished( (Long) objects[1] >  0);
      documentVersions.add( documentVersion );
   }
查询=
getEntityManager().createNamedQuery(“DocumentVersion.findByDocumentId”);
setParameter(“id”,docsFilter.getProjectId());
列表资源;
尝试
{
res=query.getResultList();
}
捕获(noresulte异常)
{
返回null;
}
//res仅包含DocumentVersion/No“boolean”的列表
我想检索结果列表,但当我对查询执行“getResultList”时,我只看到select的第一部分(DocumentVersion列表),没有看到我想要得到的布尔值

我正在使用一个最新的hibernate版本作为pesistence提供程序


谢谢。

正如SJuan指出的,exist()不能在select表达式中使用。所以我用一个左连接修改了查询,这个连接很好用。以下是查询:

SELECT p, count(dp.id) 
FROM DocumentVersion p left join  p.documentPublications dp 
where p.document.id  = :id 
group by p.id
使用代码检索结果:

   Query query =   
     getEntityManager().createNamedQuery("DocumentVersion.findByDocumentId");

   query.setParameter("id", docsFilter.getProjectId());

   List<Object[]>  res;
   try
   {
       res = query.getResultList();
   }
   catch (NoResultException e)
   {
       return null;
   }
   // res only contains a list of DocumentVersion / No 'boolean'
 List<Object[]>  res;
   try
   {
       res = query.getResultList();
   }
   catch (NoResultException e)
   {
       return null;
   }

   List<DocumentVersion> documentVersions = new ArrayList<>();
   for(Object[] objects : res)
   {
      DocumentVersion documentVersion = (DocumentVersion) objects[0];
      documentVersion.setPublished( (Long) objects[1] >  0);
      documentVersions.add( documentVersion );
   }
列表res;
尝试
{
res=query.getResultList();
}
捕获(noresulte异常)
{
返回null;
}
List documentVersions=new ArrayList();
对于(对象[]对象:res)
{
DocumentVersion DocumentVersion=(DocumentVersion)对象[0];
documentVersion.setPublished((长)对象[1]>0);
documentVersions.add(documentVersion);
}

您应该添加调用此查询的Java代码:-(…不应该问您这个问题。我一直在浏览并发现;简而言之,
exists
是条件语句的一部分,它只是
WHERE
HAVING
子句的一部分(检查答案中提供的链接).很抱歉给您带来不便。@SJuan76不要感到抱歉。您帮助了我。谢谢:)