hql连表查询的问题,总结了一下,与大家分享
package android.com.bzjm.test;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import com.bzjm.pojo.Ktvinfo;import com.bzjm.pojo.Ktvtype;import android.com.bzjm.pojo.HibernateSessionFactory;/*** @author Adayan 2016年2月21日下午5:15:53*/public class Hqltext {public void jiaocha() {// 交叉查询// 同时查询出多表的数据Session session = null;try {session = HibernateSessionFactory.getSession();Transaction ts = session.beginTransaction();Query query = session.createQuery("from Ktvinfo,Ktvtype");List<Object[]> list = query.list();// 将获取到的2个表,分开获取for (Object[] values : list) {Ktvinfo ktvinfo = (Ktvinfo) values[0];System.out.println(ktvinfo.getId() + ktvinfo.getKinformation());Ktvtype type = (Ktvtype) values[1];System.out.println(type.getKroomInfo() + type.getKleftNum());}} catch (HibernateException e) {e.printStackTrace();session.getTransaction().rollback();} finally {HibernateSessionFactory.closeSession();}}public void neilianjie() {// 内连接查询(用于有外键的情况)// 下面三种(用于有外键的情况)hql语句都是可以得到相同的结果// 1.String hql="select p from Ktvtype as p inner join p.ktvinfo";// 在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,这样的查询效率要比上面的hql语句要高// String hql="select p from Ktvtype as p inner join fetch p.ktvinfo";// 2.String hql="select p from Ktvtype p,Ktvinfo k where p.ktvinfo=k";// 3.String// hql="select p from Ktvtype p,Ktvinfo k where p.ktvinfo.id=k.id";// 没有外键的情况: String hql="from Ktvtype p,Ktvinfo k where p.id=k.id";Session session = null;try {session = HibernateSessionFactory.getSession();Transaction ts = session.beginTransaction();// 用于有外键的时候String hql = "select p from Ktvtype as p inner join fetch p.ktvinfo";Query query = session.createQuery(hql);List<Object[]> list = query.list();// 将获取到的2个表,分开获取for (Object[] values : list) {Ktvinfo ktvinfo = (Ktvinfo) values[0];System.out.println(ktvinfo.getId() + ktvinfo.getKinformation());Ktvtype type = (Ktvtype) values[1];System.out.println(type.getKroomInfo() + type.getKleftNum());}} catch (HibernateException e) {e.printStackTrace();session.getTransaction().rollback();} finally {HibernateSessionFactory.closeSession();}}public void zuowailianjie() {// 左外连接(left outer join)// String// hql="select p from Ktvtype p left outer join p.ktvinfo order by p.ktvinfo.id";// 在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,//这样的查询效率要比上面的hql语句要高// String//hql="select p from Ktvtype p left outer join fetch p.ktvinfo order by p.ktvinfo.id";// String hqlk="select k from Ktvinfo k left outer join k.ktvtype";// 在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,//这样的查询效率要比上面 的hql语句要高// String hqlk="select k from Ktvinfo k left outer join fetch k.ktvtype";Session session = null;try {session = HibernateSessionFactory.getSession();Transaction ts = session.beginTransaction();String hql = "select p from Ktvtype p left outer join fetch p.ktvinfo order by p.ktvinfo.id";Query query = session.createQuery(hql);List<Ktvtype> list = query.list();for (Ktvtype p : list) {System.out.println(p.getKroomInfo() + p.getKleftNum());}System.out.println("+++++++++++++++++++++++++++++++++++++++");String hqlk = "select k from Ktvinfo k left outer join fetch k.ktvtype";query = session.createQuery(hqlk);List<Ktvinfo> listk = query.list();for (Ktvinfo k : listk) {System.out.println(k.getKinformation() + k.getKlocaltion());}} catch (HibernateException e) {e.printStackTrace();session.getTransaction().rollback();} finally {HibernateSessionFactory.closeSession();}}public void youwailianjie() {// 右外连接(right outer join)Session session = null;try {session = HibernateSessionFactory.getSession();Transaction ts = session.beginTransaction();String hql = "select k from Ktvinfo k right outer join k.ktvtype order by k.id";Query query = session.createQuery(hql);List<Ktvinfo> listk = query.list();for (Ktvinfo k : listk) {System.out.println(k.getKinformation() + k.getKlocaltion());}} catch (HibernateException e) {e.printStackTrace();session.getTransaction().rollback();} finally {HibernateSessionFactory.closeSession();}}}