读书人

hibernate 多表查询回来新定义的对象

发布时间: 2012-09-22 21:54:54 作者: rapoo

hibernate 多表查询,返回新定义的对象
SessionFactory sessionFactory=Configuration.buildSessionFactory("hibernate.cfg.xml");
Session session=sessionFactory.currentSession();

String hql="select new A(a.ab001,a.ab002,a.ab006,b.aa002 as ab003) from db_xtwh_bmb as a left outer join db_xtwh_ryb as b on a.ab003=b.aa001 where a.ab004= "0 "";

Query query=session.createQuery(hql);
List <A > list=query.list();
return list;


hibernate 左外连接的写法:
select mother, offspr, mate.name from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr 这是正确的左连接 语句 ,但这个是在表之间配置了关系后才能使用的我想问的就是,在没配置表与表之间的关系的情况下,怎么使用HQL语句 左连接 查询?

跟sql的左外连接不一样

在hibernate中使用左外链接要先建立关联
否则只能通过一些小技巧来完成

参考:http://mayabin.iteye.com/blog/123886


这里还有另外一种左外链接的写法:
select cu.customerid,cu.enpname,cu.phone,cu.mobile,cu.EMail,sd.dname
from Customerinfomation as cu, Sysdata as sd Where sd.did(+)=cu.validflag

用(+)代替left join

http://77857.blog.51cto.com/67857/153519

hibernate 多表查询

读书人网 >软件架构设计

热点推荐