等值连接查询

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();
	}

}

猜你喜欢

转载自blog.csdn.net/kimi_n/article/details/83957432
今日推荐