Hibernate进阶学习4

Hibernate进阶学习4

深入学习hibernate的查询语句

测试HQL查询

package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import java.util.List;

/**
 * @author: 肖德子裕
 * @date: 2018/11/16 10:26
 * @description: 测试HQL语句(hibernate独有的面向对象的语法)(适合不复杂的多表查询)
 */
public class Test1 {
    /**
     * 排序查询
     */
    @Test
    public void test1(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        //1)书写HQL语句
        String hql="from Customer order by cust_id";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Customer> list = query.list();

        System.out.println(list);

        /*******************************************************/

        tx.commit();
        session.close();
    }

    /**
     * 统计查询
     */
    @Test
    public void test2(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        //1)书写HQL语句
        String hql="select count(*) from Customer";
        String hql1="select sum(cust_id) from Customer";
        String hql2="select avg(cust_id) from Customer";
        String hql3="select max(cust_id) from Customer";
        String hql4="select min(cust_id) from Customer";
        //2)创建查询对象
        Query query = session.createQuery(hql2);
        //3)执行查询
        Number number = (Number) query.uniqueResult();

        System.out.println(number);

        /*******************************************************/

        tx.commit();
        session.close();
    }

    /**
     * 投影查询
     */
    @Test
    public void test3(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        //1)书写HQL语句
        String hql="select new Customer(cust_id,cust_name) from Customer";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Customer> list = query.list();

        System.out.println(list);

        /*******************************************************/

        tx.commit();
        session.close();
    }
}
HQL
package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author: 肖德子裕
 * @date: 2018/11/19 14:35
 * @description: 测试HQL多表查询(hibernate独有的面向对象的语法)(适合不复杂的多表查询)
 */
public class Test2 {
    /**
     * 内连接
     */
    @Test
    public void test1(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        //1)书写HQL语句
        String hql="from Customer c inner join c.linkMens";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Object[]> list = query.list();

        for (Object[] obj:list){
            System.out.println(Arrays.toString(obj));
        }

        /*******************************************************/

        tx.commit();
        session.close();
    }

    /**
     * 迫切内连接
     * 将查询到的关联的对象也封装到查询的对象中
     */
    @Test
    public void test2(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        //1)书写HQL语句
        String hql="from Customer c inner join fetch c.linkMens";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Customer> list = query.list();

        System.out.println(list);

        /*******************************************************/

        tx.commit();
        session.close();
    }

    /**
     * 左(右)外连接
     */
    @Test
    public void test3(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        //1)书写HQL语句
        //String hql="from Customer c left join c.linkMens";
        String hql="from Customer c right join c.linkMens";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Object[]> list = query.list();

        for (Object[] obj:list){
            System.out.println(Arrays.toString(obj));
        }

        /*******************************************************/

        tx.commit();
        session.close();
    }
}
HQL2

测试Criteria查询

package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.*;
import org.junit.Test;

import javax.print.attribute.standard.Destination;
import java.util.List;

/**
 * @author: 肖德子裕
 * @date: 2018/11/16 10:26
 * @description: 测试criteria语句(hibernate独有的无语句的全面向对象的查询语法)(适合单表查询)
 */
public class Test3 {
    /**
     * 排序查询
     */
    @Test
    public void test(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        Criteria criteria = session.createCriteria(Customer.class);
        //criteria.addOrder(Order.asc("cust_id"));
        criteria.addOrder(Order.desc("cust_id"));
        List list = criteria.list();
        System.out.println(list);

        /*******************************************************/

        tx.commit();
        session.close();
    }

    /**
     * 离线查询
     * 就是在不创建session的情况下也能进行数据库操作(比如在service和web层调用)
     */
    @Test
    public void test1(){
        //创建离线对象
        DetachedCriteria dc=DetachedCriteria.forClass(Customer.class);
        dc.add(Restrictions.idEq(3L));

        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        Criteria criteria = dc.getExecutableCriteria(session);
        List list = criteria.list();
        System.out.println(list);

        /*******************************************************/

        tx.commit();
        session.close();
    }
}
criteria

测试类级别加载策略

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.domain" >
    <class name="Customer" table="cst_customer" lazy="false" >
        <id name="cust_id"  >
            <generator class="native"></generator>
        </id>
        <property name="cust_name" column="cust_name" ></property>
        <property name="cust_source" column="cust_source" ></property>
        <property name="cust_industry" column="cust_industry" ></property>
        <property name="cust_level" column="cust_level" ></property>
        <property name="cust_linkman" column="cust_linkman" ></property>
        <property name="cust_phone" column="cust_phone" ></property>
        <property name="cust_mobile" column="cust_mobile" ></property>
    
    <!-- 
        lazy属性: 决定是否延迟加载
            true(默认值): 延迟加载,懒加载
            false: 立即加载
            extra: 极其懒惰
        fetch属性: 决定加载策略.使用什么类型的sql语句加载集合数据
            select(默认值): 单表查询加载
            join: 使用多表查询加载集合
            subselect:使用子查询加载集合
     -->
     <!-- batch-size: 抓取集合的数量为3.
             抓取客户的集合时,一次抓取几个客户的联系人集合.
      -->
        <set name="linkMens" batch-size="3"  >
            <key column="lkm_cust_id" ></key>
            <one-to-many class="LinkMan" />
        </set>
    </class>
</hibernate-mapping>
customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.domain" >
    <class name="LinkMan" table="cst_linkman" >
        <id name="lkm_id"  >
            <generator class="native"></generator>
        </id>
        <property name="lkm_gender"  ></property>
        <property name="lkm_name"  ></property>
        <property name="lkm_phone"  ></property>
        <property name="lkm_email"  ></property>
        <property name="lkm_qq"  ></property>
        <property name="lkm_mobile"  ></property>
        <property name="lkm_memo"  ></property>
        <property name="lkm_position"  ></property>
        <!-- 
        fetch 决定加载的sql语句
            select: 使用单表查询
            join : 多表查询
        lazy  决定加载时机
            false: 立即加载
            proxy: 由customer的类级别加载策略决定.
         -->
        <many-to-one name="customer" column="lkm_cust_id" class="Customer" fetch="join" lazy="proxy"  >
        </many-to-one>
    </class>
</hibernate-mapping>
linkman.hbm.xml
package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.*;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author: 肖德子裕
 * @date: 2018/11/16 10:26
 * @description: 类级别加载策略
 */
public class Test4 {
    /**
     * 懒加载|延迟加载
     */
    @Test
    public void test(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        //立即加载
        //Customer customer = session.get(Customer.class, "2L");

        //延迟加载:查询时只返回一个代理对象,在使用时,根据关联的session查询数据库返回结果
        //为了更好的性能,建议延迟加载;延迟加载貌似只是推迟了查询
        Customer customer = session.load(Customer.class, "2L");
        System.out.println(customer);

        /*******************************************************/

        tx.commit();
        session.close();
    }
}
test

测试关联级别加载策略

package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.domain.LinkMan;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import java.util.Set;

/**
 * @author: 肖德子裕
 * @date: 2018/11/16 10:26
 * @description: 关联级别加载策略
 */
public class Test5 {
    /**
     * lazy与fetch的使用
     */
    @Test
    public void test(){
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();

        /********************* 数据库操作 **********************/

        //立即加载
        Customer customer = session.get(Customer.class, "2L");

        Set<LinkMan> linkMens = customer.getLinkMens();
        System.out.println(linkMens.size());
        System.out.println(linkMens);

        /*******************************************************/

        tx.commit();
        session.close();
    }
}
test

猜你喜欢

转载自www.cnblogs.com/xdzy/p/9984876.html