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. 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. 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?
the map is predefined to
map<column, value>notmap<column1value, column2value>you have to build the map from the resultlist