等值連線查詢
阿新 • • 發佈:2018-11-11
package com.kgc.biz; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Transaction; import com.kgc.common.HibernateSessionFactory; import com.kgc.dao.DeptDao; import com.kgc.po.Dept; public class DeptBiz { private DeptDao dao = new DeptDao(); //查詢 public void findTest(){ Transaction tx = null; try { tx=HibernateSessionFactory.getSession().beginTransaction(); /*等值連線 select * from Dept d,EMP e where d.deptno = e.deptno *錯誤寫法 * String hql = "from Dept d ,Emp e where d.emp= e"; d.emps是集合(Set)!=e是物件(Emp); */ String hql = " from Dept d ,Emp e where d= e.dept"; // hql = "select d from Dept d ,Emp e where d= e.dept"; // hql = "from Dept d ,Emp e where d.deptno= e.dept.deptno"; List<Object[]> result = dao.find(hql); for (Object[] row : result) { System.out.println(row[0]+"\t"+row[1]); } tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if(tx!=null){ tx.rollback(); } } } }
//測試類
package com.kgc.test;
import com.kgc.biz.DeptBiz;
import com.kgc.biz.EmpBiz;
import com.kgc.po.Dept;
import com.kgc.po.Emp;
public class DeptTest {
public static void main(String[] args) {
DeptBiz biz = new DeptBiz();
biz.findTest();
}
}