hibernate的NamedQuery传参数

public Query getNamedQuery(String queryName,Map<String,Object> param){
Query query = getSession().getNamedQuery(queryName);
Query rsQuery = null;
try{
  Velocity.init();
  VelocityContext context = new VelocityContext();
  if(param!=null) {
   Set<String> keys = param.keySet();
   for(Iterator<String> it = keys.iterator();it.hasNext();) {
    String key = it.next();
    context.put(key,param.get(key));
   }
  }
  StringWriter sql = new StringWriter();
  Velocity.evaluate(context,sql,null,query.getQueryString());
  rsQuery = getSession().createSQLQuery(sql.toString());
  BeanUtil.forceSetProperty(rsQuery,"queryReturns",BeanUtil.forceGetProperty(query,"queryReturns"));
}catch(Exception e) {
   e.printStackTrace();
  }
  return rsQuery;
}

你可能感兴趣的:(Hibernate)