1. 程式人生 > >命名查詢(二)

命名查詢(二)

多表命名查詢

實體類的Emp、Dept   (getter/setter  、   無參  、有參)

//Dept.hbm.xml 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.kgc.po.Dept" table="DEPT">
        <id name="deptno" type="java.lang.Short">
            <column name="DEPTNO" precision="2" scale="0" />
            <generator class="increment" />
        </id>
        <property name="dname" type="java.lang.String">
            <column name="DNAME" length="14" />
        </property>
        <property name="loc" type="java.lang.String">
            <column name="LOC" length="13" />
        </property>
        <set name="emps" cascade="save-update,delete" inverse="true">
        	<key>
        		<column name="DEPTNO"></column>
        	</key>
        	<one-to-many class="com.kgc.po.Emp"/>
        </set>
    </class>
    <query name="testNamedQuery">
    	<![CDATA[
    		from Dept d left join d.emps e where d.deptno= :deptno
    	]]>
    </query>  
</hibernate-mapping>

 

//com.kgc.dao

package com.kgc.dao;

import java.util.List;

import com.kgc.common.HibernateSessionFactory;

public class DeptDao  {
	
	public List find(String hql){
		return HibernateSessionFactory.getSession().createQuery(hql).list();
	}
	public List findByNamed(String name,Object arg){
		return HibernateSessionFactory.getSession()
				.getNamedQuery(name)
				.setParameter("deptno", arg)
				.list();
	}
}

//DeptBiz.java

package com.kgc.biz;

import java.util.List;

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

import com.kgc.common.HibernateSessionFactory;
import com.kgc.dao.DeptDao;
import com.kgc.po.Dept;
import com.kgc.po.Emp;

public class DeptBiz {
	private DeptDao dao = new DeptDao();
	//查詢
	public void findTest(){
		Transaction tx = null;
		try {
			tx=HibernateSessionFactory.getSession().beginTransaction();
			/**隱式連線查詢*/
			String hql = " from Emp e where e.dept.dname= :dname";

			Query query = HibernateSessionFactory.getSession().createQuery(hql);
           //查詢sales部門的員工
			query.setParameter("dname", "SALES");

			List<Emp> emps = query.list();

			for(Emp e:emps){

			System.out.println(e.getEname()+"\t"+e.getDept().getDname());
			}
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
	}
	
	public void testNamedQuery(){
		Transaction tx = null;
		try {
			tx=HibernateSessionFactory.getSession().beginTransaction();
			List<Object[]> result = dao.findByNamed("testNamedQuery", (short)10);
			for (Object[] e : result) {
				System.out.println(e[0]+"\t"+e[1]);
			}
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
	}
}

 //DeptTest.java

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

}

//執行結果


Hibernate: 
    select
        dept0_.DEPTNO as DEPTNO1_0_,
        emps1_.EMPNO as EMPNO0_1_,
        dept0_.DNAME as DNAME1_0_,
        dept0_.LOC as LOC1_0_,
        emps1_.ENAME as ENAME0_1_,
        emps1_.JOB as JOB0_1_,
        emps1_.MGR as MGR0_1_,
        emps1_.HIREDATE as HIREDATE0_1_,
        emps1_.SAL as SAL0_1_,
        emps1_.COMM as COMM0_1_,
        emps1_.DEPTNO as DEPTNO0_1_ 
    from
        DEPT dept0_ 
    left outer join
        EMP emps1_ 
            on dept0_.DEPTNO=emps1_.DEPTNO 
    where
        dept0_.DEPTNO=?
[email protected]	[email protected]
[email protected]	[email protected]
[email protected]	[email protected]