首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hibernate sqlQuery 与实体类的映射关系有关问题

2012-04-24 
hibernate sqlQuery 与实体类的映射关系问题sql语句:selectcs.funid ,cs.funname,cs.describe,cs.parents,

hibernate sqlQuery 与实体类的映射关系问题
sql语句:  
select
  cs.funid ,
  cs.funname,
  cs.describe,
  cs.parents,
  (select count(1) from CodeSysFunction1 csf where csf.parents=cs.funid) children 
from
  CodeSysFunction1 cs 
where parents = 0

执行这个SQl语句的方法:
public List<CodeSysFunction> queryForList(String sql){
return this.getSession().createSQLQuery(sql).addEntity(CodeSysFunction.class).list();//sql语句
}

现在问题是这个语句的查询结果不能和CodeSysFuncion这个类映射。加上.addEntity(CodeSysFunction.class)后报java.sql.SQLException: 列名无效错误,去掉.addEntity(CodeSysFunction.class)查出的结果是object。

CodeSysFunction类属性:
//功能ID
private int funid;

//功能代码
private String funcode;

//功能描述
private String describe;

//父节点id
private int parents;

实体类及数据库内都没children字段。感觉是这个地方的问题。报列明无效错误估计就是因为这么原因。还是SQLQuery原因。大神们给看下。


[解决办法]
代码逻辑混乱。。。
你方法返回值是List<CodeSysFunction>,CodeSysFunction对象中不包含children属性
也就是即使不报错,你的方法返回的是List<CodeSysFunction>,用不到children数据。
既然用不到,为什么你的SQL要将它查出来。。。
[解决办法]
在CodeSysFunction里面加一个children属性。
[解决办法]
(select count(1) from CodeSysFunction1 csf where csf.parents=cs.funid) children改成(select count(1) from CodeSysFunction1 csf where csf.parents=cs.funid) as children
[解决办法]
where parents = 0改成where cs.parents = 0呢。
[解决办法]
没觉得少什么条件啊。
[解决办法]

探讨

引用:
代码逻辑混乱。。。
你方法返回值是List<CodeSysFunction>,CodeSysFunction对象中不包含children属性
也就是即使不报错,你的方法返回的是List<CodeSysFunction>,用不到children数据。
既然用不到,为什么你的SQL要将它查出来。。。



列名无效的那个问题已经解决了。但是……

热点排行