深入理解hibernate中的API

这里我对hibernate中的一些API进行了一些详细的理解与分析:

package com.hibernate.demo1;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import com.hibernate.utils.HibernateUtils;

/***
 * hibernate中的API详解
 * @author Administrator
 *
 */
public class HibernateTest2 {


    @Test
    /****
     * Session对象------>save(arg);
     */
    public void demo1(){

        //1通过工具类获取Session
        Session session = HibernateUtils.openSession();
        //2开启事务
        Transaction tx = session.beginTransaction();
        //5操作
        //向数据库插入一条记录
            Customer customer = new Customer();
            customer.setName("小王2");
            customer.setAge(26);
            session.save(customer);
        //6事务提交
        tx.commit();
        //7释放资源
        session.close();
    }

    @Test
    //保存或更新
    /****
     * Session对象------>saveOrUpdate(arg);
     */
    public void demo2(){

        //1通过工具类获取Session
        Session session = HibernateUtils.openSession();
        /*2开启事务 【 如果没有这句话和tx.commit(),就没有开启事务,
                         因此以下每执行一个对数据库的操作都是一个单独的事务(所有操作都不在一个事务里),
                         并且每次执行完一个事务都会自动回滚】*/
        Transaction tx = session.beginTransaction();
        //5操作
        //向数据库插入一条记录
            Customer customer = new Customer();
            customer.setName("小王4");
            customer.setAge(26);
            /*使用【saveOrUpdate(customer)】对数据库进行【插入或更新】,
                 如果数据库已经存在与之对应的对象会执行【update】操作,
                 如果不存在则执行【save】操作*/
            session.saveOrUpdate(customer);
        //检测提交前是否提交事务操作 false   
        System.out.println(tx.wasCommitted());
        //6事务提交
        tx.commit();
        //检测提交后是否提交事务操作 true
        System.out.println(tx.wasCommitted());
        //7释放资源
        session.close();
    }

    @Test
    /****
     * Query对象------>HQL操作
     */
    public void demo3(){

        //1通过工具类获取Session
        Session session = HibernateUtils.openSession();
        //2开启事务
        Transaction tx = session.beginTransaction();
        //5操作
            /** HQL查询所有操作,无条件查询【不带参数】【Customer代表Customer对象,要与对象名称一致】 **/
            //Query query = session.createQuery("from Customer");
            /** HQL查询操作,有条件查询【带参数】 **/
            //Query query = session.createQuery("from Customer where name = ?");
            //query.setParameter(0, "小李");//设置第一个?参数为"小李"【下标从0开始】
            /** HQL分页查询操作 **/
            Query query = session.createQuery("from Customer");
            //查询前3条记录
            query.setFirstResult(0);
            query.setMaxResults(3);

            List list = query.list();
            for(Customer customer:list){
                System.out.println(customer);
            }
        //6事务提交
        tx.commit();
        //7释放资源
        session.close();
    }

    @Test
    /****
     * Criteria对象------>QBC条件查询操作
     */
    public void demo4(){

        //1通过工具类获取Session
        Session session = HibernateUtils.openSession();
        //2开启事务
        Transaction tx = session.beginTransaction();
        //5操作
        //条件查询操作
            /** QBC查询所有操作,简单查询操作 **/
            //Criteria creteria = session.createCriteria(Customer.class);

            /** QBC查询操作,条件查询操作 **/
            //Criteria creteria = session.createCriteria(Customer.class);
            //creteria.add(Restrictions.eq("name", "小王"));//设置条件name=小王

            /** QBC查询操作,分页查询操作 **/
            Criteria creteria = session.createCriteria(Customer.class);
            creteria.setFirstResult(0);
            creteria.setMaxResults(3);

            List list = creteria.list();
            for(Customer customer:list){
                System.out.println(customer);
            }
        //6事务提交
        tx.commit();
        //7释放资源
        session.close();
    }



    @Test
    /****
     * 演示一个错误【经常发生】customer1的ID=customer2的ID
     */
    public void demo5(){

        //1通过工具类获取Session
        Session session = HibernateUtils.openSession();
        //2开启事务
        Transaction tx = session.beginTransaction();
        //5操作
        //customer1的ID不能=customer2的ID
            Customer customer1 = (Customer) session.get(Customer.class, 6);
            Customer customer2 = new Customer();
            customer2.setId(6);
            customer2.setName("张飞");
            session.update(customer2);
        //6事务提交
        tx.commit();
        //7释放资源
        session.close();
    }

    @Test
    /****
     * 演示持久化类为final情况
     */
    public void demo6(){

        //1通过工具类获取Session
        Session session = HibernateUtils.openSession();
        //2开启事务
        Transaction tx = session.beginTransaction();
        //5操作
        //将Customer用final修饰
            /** customer的对象为真实对象,而不是代理对象了 **/
            Customer customer = (Customer) session.load(Customer.class, 6);
        //6事务提交
        tx.commit();
        //7释放资源
        session.close();
    }
}

你可能感兴趣的:(hibernate,api,hibernate,Criteria,Query,Session)