使用jpa的原生sql,将返回的记过封装到list map中是用的工具类
由于jpa对于左连接不支持,只能写原生的sql
hql.append("SELECT u.id as id,u.account as account,u.name as userName,r.name as roleName,")
.append(sql2)
.append(",u.cardNo as cardNo,u.email as email,u.printAuth as printAuth,u.colorAuth as colorAuth")
.append(" from user as u")
.append(" LEFT JOIN department d ON u.departmentId=d.id LEFT JOIN role r ON u.roleId=r.id");
query=em.createNativeQuery("select count(u.id) from user as u LEFT JOIN department d ON u.departmentId=d.id LEFT JOIN role r ON u.roleId=r.id");
public static List<HashMap<String, Object>> listArrayToListMap(List<Object> list,String... keys) {
if(list==null||list.size()==0){
return null;
}
List<HashMap<String, Object>> mapList = new ArrayList<HashMap<String,Object>>();
for(int i=0;i<list.size();i++){
if(!(list.get(i) instanceof Map)){
HashMap<String, Object> map = new HashMap<String, Object>();
Object[] m = (Object[])list.get(i);
for(int j=0;j<keys.length;j++){
map.put(keys[j],m[j]);
}
mapList.add(map);
}
}
return mapList;
}
关于jps原生sql
query=em.createNativeQuery("select count(u.id) from user as u LEFT JOIN department d ON u.departmentId=d.id LEFT JOIN role r ON u.roleId=r.id");
BigInteger bi= (BigInteger) query.getSingleResult();
返回类型是BigInteger
而HSQL返回的是LONG类型