繁体   English   中英

如何获取JPA(休眠)查询结果列表作为HashMap?

[英]How to get jpa(hibernate) query resultlist as HashMap?

目前,我的任务是为客户端解析器准备文档服务,以便用户可以查看,编辑,管理仅允许的文档。 Documentprivileges对于文档模型具有@onetomany关系。 我已按文件添加了获得单一特权

  public String getDocumentPrivilege(Long documentId);
    ......

我也想通过重写方法返回HashMap(docId和特权)。 到目前为止,我已经完成了:

@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;
    }

但我得到以下错误:

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

我检查了其他例子,这是非常相似的。 我是否遵循错误的方式?

您必须使用setParameterList在查询中设置要与HQL的运算符'IN'一起使用的documentId列表。

映射已预定义为map<column, value>而不是map<column1value, column2value>您必须从结果列表中构建映射

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM