1. 程式人生 > >Hibernate查詢-不迫切左連線與迫切的區別

Hibernate查詢-不迫切左連線與迫切的區別

這裡寫圖片描述
不迫切一行一個數組
d1只建立一次
封裝成陣列裝進List

類+關聯屬性,而不是類了。

public List find(String hql) {
        return HibernateSessionFactory.getSession()
                .createQuery(hql)
                .list();
    }
package biz;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import
dao.DeptDAO; import dao.HibernateSessionFactory; import entity.Dept; public class DeptBiz { private DeptDAO dao = new DeptDAO(); public void findTest() { Transaction tx = null; Dept dept = null; try { tx = HibernateSessionFactory.getSession().beginTransaction(); /* * select * from Dept d left join Emp e on d.deptno = e.deptno where * ... */
// String hql = "from Dept d left join Emp e"; String hql = "from Dept d left join d.emps e order by d.deptno";// 省略以上的 List<Object[]> result = dao.find(hql); //不迫切查出來封裝成陣列物件 然後放進List中 // fetch迫切查出結果集是一樣的,主要是封裝不一樣。 for (Object[] row : result) { System.out.println(row[0
]+"\t"+row[1]); } tx.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (tx != null) { tx.rollback(); } } }
@Test 
    public void test5() {
        DeptBiz biz = new DeptBiz();
        biz.findTest();

    }

迫切

package biz;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import dao.DeptDAO;
import dao.HibernateSessionFactory;
import entity.Dept;

public class DeptBiz {
    private DeptDAO dao = new DeptDAO();

    public void findTest() {
        Transaction tx = null;
        Dept dept = null;
        try {
            tx = HibernateSessionFactory.getSession().beginTransaction();
            /*
             * select * from Dept d left join Emp e on d.deptno = e.deptno where
             * ...
             */
            // String hql = "from Dept d left join Emp e";
            String hql = "from Dept d left join fetch d.emps e order by d.deptno";// 省略以上的
            List<Dept> result = dao.find(hql);
            for (Dept dept1 : result) {
                System.out.println(dept1.getDeptno() + "\t" + dept1.getDname()
                        + "\t" + dept1.getEmps().size());
            }
            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (tx != null) {
            tx.rollback();
        }

    }
}

這裡寫圖片描述
沒有右迫切
因為右是完整的表,封裝時時以左邊為準,你不可能讓它去找null做對應吧~

// 根據部門名稱 查出所有該部門的僱員
            // 等值連線 select * from Dept d,Emp e where d.deptno=e.deptno;
            hql = "select e from Emp e,Dept d where d=e.dept and d.dname= ?";
            hql = "from Dept d,Emp e where d=e.dept";
            hql = "from Dept d,Emp e where d.deptno=e.dept.deptno";
            // WRONG!!
            hql = "from Dept d,Emp e where d.emps=e";
            // emps是一個集合,不可以等於一個物件(資料型別都不對!) set!=Emp
            // 隱式內連線
            hql = "from Emp e where e.dept.dname= :dname";

            /*內連線
             * select e.EMPNO,e.ENAME,d.DNAME
             * from EMP e inner join DEPT d on e.DEPTNO = d.DEPTNO;
             * where....
             */