日期:2014-05-17  浏览次数:20714 次

hibernate两表 连查。高手速度,来结帖!
String hql=" from TbSysProduct as b , TbSysService a where b.tfid in elements ( a.tfId ) and a.tbSysDept.tfId = "+tbSysUser.getTbSysDept().getTfId()+")";



报错。


该如何写?

------解决方案--------------------
两个表的关系木有说清楚。
------解决方案--------------------
把最后面的")"去掉
------解决方案--------------------
String hql="select b from TbSysProduct b , TbSysService a where b.tfid = a.tbSysDept.tfId and a.tbSysDept.tfId = "+tbSysUser.getTbSysDept().getTfId()";
  
试试看。 如不行那就写sql语句把。 有方法可以把你查询出的list<a> , a 自动转成TbSysProduct

语句就是Query query = session.createSQLQuery(sql).addEntity(表名,实体类.class).addEntity(表名,实体类.class) 两个表就写两个。 一张表就写一个。
------解决方案--------------------
忘了 sql 的query 是SQLQuery 你换下。
------解决方案--------------------
String hql=" from TbSysProduct as tp , TbSysService as ts where tp.tfid = ts.tfId and tp.tbSysDept.tfId = "+tbSysUser.getTbSysDept().getTfId()+" )";


保持实体类1 和实体类2 的id对应上就可以了。然后在做条件判断
TbSysProduct.id = TbSysService.id where TbSysProduct.tbSysDept.tfId = XXX 就行


貌似俩个表连查还有一个表里是一对多嘞、

------解决方案--------------------
String hql=" from TbSysProduct as b , TbSysService a where b.tfid = a.tfId[b][/b]
 and a.tbSysDept.tfId = "+tbSysUser.getTbSysDept().getTfId()+")";

为什么不直接用and?