Hibernate Session在项目中的创建方式

创建一个单例模式工具类,再通过在Hibernate配置文件中开启Session的线程管理模式的方法

  1. 单例模式工具类
    package test.hibernate.spring.dao;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.MetadataSources;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    
    public class HibernateUtil {
    	public static HibernateUtil hibernateUtil = null;
    
    	public HibernateUtil() {};
    	//将该类作成单例模 式
    	public static HibernateUtil getinstance() {
    		if(hibernateUtil==null) {
    			hibernateUtil=new HibernateUtil();
    		}
    		return hibernateUtil;
    	}
    	public SessionFactory getSessionFactory() {
    		/* hibernate规定,所有的配置或服务,必须配置或注册到一个服务注册类中 */
    		Configuration configuration = new Configuration().configure();
    		ServiceRegistry sr = configuration.getStandardServiceRegistryBuilder().build();
    		/* 从注册类中获得工厂类 */
    		return new MetadataSources(sr).buildMetadata().buildSessionFactory();
    	}
    
    	/* 获取session的对像方法 */
    	public Session getSession() {
    		/* 从单前线程中获取Session,session在完成操作后将会自动关闭 */
    		return getSessionFactory().getCurrentSession();
    		
    	}
    }

     
  2. Hibernate配置文件中开启Session的线程管理模式的方法
    <!-- 开启Session的线程管理模式 -->
      <property name="current_session_context_class">thread</property>

  3. 测试应用
    public void testGet() {
    	
    		Session session=HibernateUtil.getinstance().getSession();
    		Transaction transaction=session.beginTransaction();
    		
    		Student s = session.get(Student.class, 1);
    		System.out.println(s.getTeachers().size());
    		
    		transaction.commit();
    	}

    手动创建方式,以一个测试类为例
     

    public class TestSession {
    	SessionFactory sessionFactory = null;
    	Session session = null;
    	Transaction ts = null;
    
    	@Before
    	public void beforP() {
    		System.out.println("begin....");
    		/* hibernate规定,所有的配置或服务,必须配置或注册到一个服务注册类中 */
    		Configuration configuration = new Configuration().configure();
    		ServiceRegistry sr = configuration.getStandardServiceRegistryBuilder().build();
    		/* 从注册类中获得工厂类 */
    		sessionFactory = new MetadataSources(sr).buildMetadata().buildSessionFactory();
    		/* 通过工厂类开启Session */
    		session = sessionFactory.openSession();
    		/* 开启事务 */
    		ts = session.beginTransaction();
    	}
    
    	@After
    	public void endP() {
    		System.out.println("end....");
    		/* 提交事务 */
    		ts.commit();
    		/* 关闭Session */
    		session.close();
    		/* 关闭工厂 */
    		sessionFactory.close();
    	}
      	@Test
    	public void testGet() {
    		// 使用get时, class标签中lazy值不管是true还是false都会直接加载,当set标签
    		// 中fetch="join"时,会在get时使用左join将其集体一起检索出来,访问数据库只需一次
    		// fetch值是其它时则需要访两次
    		Student s = session.get(Student.class, 1);
    		System.out.println(s.getTeachers().size());
    	}
    
    }

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值