Hibernate-HQL查询(1)基本查询、命名参数

package junit.test;

import hibch05.dao.HibernateSessionFactory;
import hibch05.pojo.Dept;
import hibch05.pojo.Emp;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;

public class Ch05Test2 {

    @Test
    public void test() {
        // 获取与数据库的连接
        Session session = HibernateSessionFactory.getSession();
        // 获取Query对象
        Query query = session.createQuery("from Dept d where d.dname like '%A%'");
        // 获取结果集
        /*List list = query.list();
        for (Dept dept : list) {
            System.out.println(dept.getDname());
            Set emps = dept.getEmps();
            // Iterator it=emps.iterator()
            for (Emp emp : emps) {
                System.out
                        .println("--" + emp.getEmpno() + " " + 

emp.getEname());
            }
        }*/

        // 调用Query对象的iterator
        Iterator it = query.iterate();
        while(it.hasNext()){
            Dept dept=it.next();
            System.out.println(dept.getDname());
        }
    }

    @Test
    public void test2() {
        // 获取与数据库的连接
        Session session = HibernateSessionFactory.getSession();
        String hql="select dname,loc from Dept";
        Query query=session.createQuery(hql);
        List list=query.list();
        for (Object[] o : list) {
            System.out.println(o[0]+" "+o[1]);
        }
    }

    @Test
    public void test3() {
        // 获取与数据库的连接
        Session session = HibernateSessionFactory.getSession();
        String hql="select new Dept(dname,loc) from Dept";
        Query query=session.createQuery(hql);
        List list=query.list();
        for (Dept dept : list) {
            System.out.println(dept.getDname()+" "+dept.getLoc());
        }
    }

    /*
     * 更新数据
     */
    @Test
    public void test4(){
        // 获取与数据库的连接
                Session session = HibernateSessionFactory.getSession();
                String hql="update Dept set dname='IT2' where deptno=1";
                Query query=session.createQuery(hql);
                int  ret=query.executeUpdate();
                if(ret>0){
                    System.out.println("OK");
                }
                session.beginTransaction().commit();
    }


    /*
     * delete
     */

    @Test
    public void test5(){
        // 获取与数据库的连接
                Session session = HibernateSessionFactory.getSession();
                String hql="delete Dept where deptno=1";
                Query query=session.createQuery(hql);
                int  ret=query.executeUpdate();
                if(ret>0){
                    System.out.println("OK");
                }
                session.beginTransaction().commit();
    }

    /*
     * ?
     */

    @Test
    public void test6(){

                Session session = HibernateSessionFactory.getSession();
                // 获取Query对象
                Query query = session.createQuery("from Dept d where d.dname like 

?");
                query.setString(0, "%A%");
                List list=query.list();
                System.out.println(list.size());
    }

    /*
     * 命名参数
     */

    @Test
    public void test7(){

                Session session = HibernateSessionFactory.getSession();
                // 获取Query对象
                Query query = session.createQuery("from Dept d where d.dname like 

:dname");
                query.setString("dname", "%A%");
                List list=query.list();
                System.out.println(list.size());
    }

}

你可能感兴趣的:(Hibernate)