2012-02-15 59 views
0

目前我的任務是爲客戶端解析器準備文檔服務,用戶可以查看,編輯,管理剛纔允許的文檔。 Documentprivileges對於文檔模型具有@onetomany關係。我已添加文件獲得單一特權如何將jpa(hibernate)查詢結果列表作爲HashMap?

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

我也想通過overriden方法返回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 

我已經檢查了其他的例子是非常相似的。我遵循錯誤的方式嗎?

回答

0

地圖被預定義爲map<column, value>map<column1value, column2value>你必須從resultlist建立映射

0

必須使用setParameterList,設置documentId列表與HQL的運營商「IN」中使用,在查詢。