现在的位置: 首页 > 综合 > 正文

hibernate使用hql多表关联查询list转为json报错

2018年05月22日 ⁄ 综合 ⁄ 共 2754字 ⁄ 字号 评论关闭


----------------   No row with the given identifier exists

1.数据库中int格式的字段为null值

2.many - to -many 关联时 第二个表中有第一个表没有的值 就会报这个错,把第二个表中的多余数据删掉 

其中也会出现的问题就是需要加上

 @JoinTable(name="userrole",inverseJoinColumns={@JoinColumn(name="roleId")},joinColumns={@JoinColumn(name="userId",insertable = false, updatable = false)})

这是在实体类user中没有roleId这个属性

//多对一
 @ManyToOne(cascade={CascadeType.REFRESH,CascadeType.MERGE},fetch=FetchType.LAZY ,optional = false)
    @JoinColumn(name = "officeId")    
 private Office office ;//科室
 @ManyToMany(cascade={CascadeType.MERGE,CascadeType.REFRESH},fetch=FetchType.LAZY)
 @JoinTable(name="userrole",inverseJoinColumns={@JoinColumn(name="roleId")},joinColumns={@JoinColumn(name="userId",insertable = false, updatable = false)})
    private Set<Role> roles=new HashSet<Role>(0);

使用hibernate多对象关联时实体类映射出现错误可使用以下查询方式

String sql = "select us.id,us.userName,us.userPassword,us.sex,us.email,us.realName,us. qq,us.isEnable,us.online,us.Score,us.content,us.createTime,isBetter,role.roleId ,re.roleName,of.officeInfo,of.id officeId " +
"from userinfo as us left join userrole as role on role.userId=us.id left join role as re on role.roleId = re.id left join office as of on us.officeId = of.id " +
"Order By createTime desc limit "+ start + "," + limit + "";

List<Map<String,Object>> userList = this.userDAO.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();

将查询数据封装到map形式的list中 然后转换为json格式,不使用hibernate的对象映射 传到前台 extjs 作为grid数据源

String result = "";
int total = this.findCountBySql("from UserInfo");
Map<String, Object> map = new HashMap<String, Object>();
try {
if (userList.size() > 0) {
map.put("total", total);
map.put("rows", userList);
result = JSONUtil.serialize(map);
} else {
map.put("total", total);
map.put("rows", "");
result = JSONUtil.serialize(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;

完整代码

public String getUserInfo(int start, int limit, Map<String, Object> paramMap) {
String sql = "select us.id,userName,userPassword,sex,email,realName, qq,isEnable,online,us.Score,us.content,us.createTime," +
"isBetter,role.roleId,re.roleName,of.officeInfo,of.id officeId " +
"from userinfo as us left join userrole as role on role.userId=us.id left join role as re on role.roleId = re.id left join office as of on us.officeId = of.id " +
"Order By createTime desc limit "+ start + "," + limit + "";
//List<UserInfo> userList = this.userDAO.createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(UserInfo.class)).list();
List<Map<String,Object>> userList = this.userDAO.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();


String result = "";
int total = this.findCountBySql("from UserInfo");
Map<String, Object> map = new HashMap<String, Object>();
try {
if (userList.size() > 0) {
map.put("total", total);
map.put("rows", userList);
result = JSONUtil.serialize(map);
} else {
map.put("total", total);
map.put("rows", "");
result = JSONUtil.serialize(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

抱歉!评论已关闭.