Hibernate ORM框架——项目一:Hibernate事务的生命周期,不建议使用openSession而改造

数据库

use demo;

select * from CLASSINFO;
select * from persons;

delete from CLASSINFO;
delete from persons;

insert into CLASSINFO values('c111','banji1');
insert into CLASSINFO values('c222','banji2');


insert into persons values('p1','p111','c111')
insert into persons values('p2','p222','c111')
insert into persons values('p3','p333','c222')
insert into persons values('p4','p444','c222')
insert into persons values('p5','p555',null)

select pid,name from persons order by pid desc;--降序
select pid,name from persons order by pid asc;--升序


select COUNT(*),name from persons group by name

select (select pid,name from persons),p.rci from persons p left join CLASSINFO c on p.rci=c.cid group by p.rci;

 

使用双向一对多多对一的案例

实体人,班级不变;实体映射文件不变;配置文件不变

 

班级dao包

package dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import shuang.m2oo2m.ClassInfo;
import util.HibernateUtil;

public class ClassInfoDao {
    /*public void insert(){
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        
        ClassInfo ci = new ClassInfo();
        ci.setCid("xxx");
        ci.setName("xxxx");
        
        s.save(ci);
        
        tx.commit();
        s.close();
        
    }*/
    
    
    /*public void insert(Session s,Transaction tx){
        SessionFactory sf = HibernateUtil.getSessionFactory();
        //Session s = sf.openSession();
        //Transaction tx = s.beginTransaction();
        
        ClassInfo ci = new ClassInfo();
        ci.setCid("xxx");
        ci.setName("xxxx");
        
        s.save(ci);
        
    //    tx.commit();
        //s.close();
        
    }*/
    
    //4.
    public void insert(){
        
        Session s = HibernateUtil
                .getSessionFactory()
                .getCurrentSession();
        System.out.println("第二次调用getCurrentSession" + s.hashCode());
        ClassInfo ci = new ClassInfo();
        ci.setCid("xxx");
        ci.setName("xxxx");
        
        s.save(ci);
        
    
        
    }
}

 

 

人dao包

package dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import shuang.m2oo2m.*;
import util.HibernateUtil;

public class PersonDao {
    
    /*
     * 1.往person表里面添加一条记录
     */
    /*public void insert(){
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        
        Person p = new Person();
        p.setPid("p6");
        p.setName("xxx");
        
        s.save(p);
        
        tx.commit();
        s.close();
        
    }*/
    
    
    //2.
    /*public void insert(Session s){
        SessionFactory sf = HibernateUtil.getSessionFactory();
        
        Transaction tx = s.beginTransaction();
        
        Person p = new Person();
        p.setPid("p6");
        p.setName("xxx");
        
        s.save(p);
        
        tx.commit();
        //s.close();
        
    }*/
    
    //3
    /*public void insert(Session s,Transaction tx){
        SessionFactory sf = HibernateUtil.getSessionFactory();
        
        //Transaction tx = s.beginTransaction();
        
        Person p = new Person();
        p.setPid("p6");
        p.setName("xxx");
        
        s.save(p);
        
        //tx.commit();
        //s.close();
        
    }*/
    
    //4.
    public void insert(){
        Session s = HibernateUtil
                .getSessionFactory()
                .getCurrentSession();
        
        System.out.println("第1111次调用getCurrentSession" + s.hashCode());
        Person p = new Person();
        p.setPid("p6");
        p.setName("xxx");
        
        s.save(p);
        
    }
}

 

业务

package service;



import dao.ClassInfoDao;
import dao.PersonDao;
import util.HibernateUtil;

public class ClassPersonService {
    /*
     * 这个方法的作用是用来
     * 同时插入一个人和一个班级信息
     */
    private PersonDao pdao= new PersonDao();
    private ClassInfoDao cdao = new ClassInfoDao();
    
    
    public void insert(){
        //1.第一次,用来表明为什么session
        //要从service类里传到dao里
        
        //初始的时候,每一个dao的方法都有自己的session与事务对象
        //也就是每个方法都是独立的
        
        //每次一个业务,对应多个dao对象的多个方法或者
        //一个dao对象的多个方法,都打开了多个session(关联了jdbc connection)
        //这样是资源浪费
        
        //同时也导致是不同的事务
        
        /*pdao.insert();
        cdao.insert();*/
        
        //2.第二次改造
        //这次的改造是让多个方法用同一个session
        //解决了多session(意味着多个jdbc connection)的
        //资源浪费问题
        
        //但是仍然存在不同同一个事务里面的问题
        /*Session s = HibernateUtil.getSessionFactory().openSession();
        pdao.insert(s);
        cdao.insert(s);
        s.close();*/
        
        //3.下面的这次改造就是同一个session,同一个事物。
        //解决了session的资源浪费问题以及多个不同事务的问题
        
    /*    Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = s.beginTransaction();
        pdao.insert(s,tx);
        cdao.insert(s,tx);
        tx.commit();
        s.close();*/
        
        //4.
    /*    Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = s.getTransaction();
        pdao.insert();
        cdao.insert();
        tx.commit();*/
        
        
    /*    Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.getTransaction().begin();//创建一个事务
        pdao.insert();
        cdao.insert();
        s.getTransaction().commit();
        */

        HibernateUtil.getSessionFactory()
        .getCurrentSession()
        .getTransaction()
        .begin();//创建一个事务
        
        pdao.insert();
        cdao.insert();
        
        HibernateUtil.getSessionFactory()
        .getCurrentSession()
        .getTransaction()
        .commit();//当事务提交时,此事务关联的session会自动帮你关闭
        
        /*与此服务类的代理类配合使用
         * pdao.insert();
        cdao.insert();*/
    }
}

 

Main测试

package com.nf;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import service.ClassPersonService;
import util.HibernateUtil;

public class Main {

    public static void main(String[] args) {
        
        ClassPersonService service = new ClassPersonService();
        service.insert();
    }

    private static void template() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();

        tx.commit();
        s.close();
        sf.close();
    }

}

 

 

 

使用代理改进版

前面不变,业务变

package service;

import util.HibernateUtil;

public class ClassPersonServiceProxy extends ClassPersonService {

    public void insert() {
        HibernateUtil.getSessionFactory()
        .getCurrentSession()
        .getTransaction()
        .begin();//创建一个事务
        
        super.insert();
        
        HibernateUtil.getSessionFactory()
        .getCurrentSession()
        .getTransaction()
        .commit();
    }
}

 

Main测试变

package com.nf;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import service.ClassPersonServiceProxy;
import util.HibernateUtil;

public class Main {

    public static void main(String[] args) {

        ClassPersonServiceProxy service = new ClassPersonServiceProxy();  
        service.insert();
    }

    private static void template() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();

        tx.commit();
        s.close();
        sf.close();
    }

}

 

posted @ 2017-08-28 21:49  梁宝谊  阅读(394)  评论(0编辑  收藏  举报