[英]How to get jpa(hibernate) query resultlist as HashMap?
Currently I have tasked to prepare Document service for client side parser that user can view, edit, manage just allowed documents. 目前,我的任务是为客户端解析器准备文档服务,以便用户可以查看,编辑,管理仅允许的文档。 Documentprivileges has @onetomany relationship for documents model. Documentprivileges对于文档模型具有@onetomany关系。 I have added get single privilege by document 我已按文件添加了获得单一特权
public String getDocumentPrivilege(Long documentId);
......
I also want to return HashMap (docId and privilege) via overriden method. 我也想通过重写方法返回HashMap(docId和特权)。 So far I have done: 到目前为止,我已经完成了:
@Override
public HashMap<Long, String> getDocumentPrivilege(List<Long> documentIds)
{
Query q = null;
if (documentIds != null && documentIds.size()>0) {
q = em.createQuery("select new map(d.document_id as id, d.privilege as privilege) from DocumentPrivileges d"
+ " where d.document_id IN ?1");
q.setParameter(1, documentIds);
@SuppressWarnings("unchecked")
HashMap<Long, String> results = (HashMap<Long, String>) q.getResultList();
if(results != null && results.size()>0)
return results;
}
return null;
}
But I am getting below error: 但我得到以下错误:
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: : near line 1, column 140 [select new map(d.document_id as id, d.privilege as privilege) from xxx.xxxx.xxxx.xxxmodel.DocumentPrivileges d where d.document_id IN :docs]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:268)
... 136 more
I have checked other examples it was quite similar. 我检查了其他例子,这是非常相似的。 Am I following wrong way? 我是否遵循错误的方式?
您必须使用setParameterList在查询中设置要与HQL的运算符'IN'一起使用的documentId列表。
映射已预定义为map<column, value>
而不是map<column1value, column2value>
您必须从结果列表中构建映射
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.