Hibernate基础(三)--核心API(一)

本文详细介绍了Hibernate中的核心API,包括Configuration类的配置信息管理和*.hbm.xml映射文件加载,SessionFactory的会话工厂创建及特点,以及Session接口的重要方法如get()、load()、save()等,探讨了它们之间的区别和使用场景。
摘要由CSDN通过智能技术生成

一、Configuration类

configuration主要用于hibernate框架加载映射文件

1.详细介绍

  1. 负责管理hibernate的配置信息

  2. 读取hibernate.cfg.xml

  3. 加载hibernate.cfg.xml配置文件中配置的驱动,url,用户名,密码,连接池.

  4. 管理 *.hbm.xml对象关系文件.

2.示意代码:

Configuration cfg=new Configuration().configure();

默认从src下读取hibernate.cfg.xml配置文件。

也可指定文件路径

Configuration cfg=new Configuration().configure("/config/hibernate.cfg.xml");

二、SessionFactory(会话工厂) 接口

1.详细介绍

  1. 缓存sql语句和某些数据

  2. 在应用程序初始化的时候创建,是一个重量级的类(吃内存),一般用单例模式保证一个应用中只需要一个 SessionFactory实例.

  3. 如果某个应用访问多个数据库,则要创建多个会话工厂实例,一般是一个数据库一个会话工厂实例.

  4. 通过SessionFactory接口可以获得Session(会话)实例.

2.示意代码:

Configuration cf=new Configuration().configure();
 SessionFactory sf=cf.buildSessionFactory();
 Session s=sf.getCurrentSession();
 //或者是: Session s=sf.openSession();

3.SessionFactory的特点

  • 它是线程安全的,它的同一个实例能够供多个线程共享。

  • 它是重量级的,不能随意的创建和销毁它的实例。

4.hibernate的工具类

package cn.utils;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
    private static final Configuration configuration;
    private static final SessionFactory sessionFactory;

    static{
        configuration=new Configuration().configure();
        sessionFactory=configuration.buildSessionFactory();
    }
    public static Session openSession(){
        return sessionFactory.openSession();
    }

}

SessionFactory内部还维护了一个连接池,如果我们需要使用第三方的连接池,如C3P0,那么我们要自己手动进行配置。

这里写图片描述

<!--C3P0配置 -->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <!--在连接池中所有数据库连接的最大数目-->
        <property name="hibernate.c3p0.max_size">20</property>
        <!--在连接池中可用的数据库连接的最小数目-->
        <property name="hibernate.c3p0.min_size">5</property>
        <!--设定数据库连接的过期时间,以秒位单位
            如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
        <property name="hibernate.c3p0.timeout">120</property>
        <property name="automaticTestTable">Test</property>
        <property name="hibernate.c3p0.max_statements">100</property>
        <!--每3000秒检查所有连接池中的空闲连接,以秒位单位-->
        <property name="hibernate.c3p0.idle_test_period">120</property>
        <property name="hibernate.c3p0.acquire_increment">1</property>
        <property name="c3p0.testConnectionOnCheckout">true</property>
        <property name="c3p0.idleConnectionTestPeriod">18000</property>
        <property name="c3p0.maxIdleTime">25000</property>
        <property name="c3p0.idle_test_period">120</property>

三、session接口

1.详细介绍

  1. Session一个实例代表与数据库的一次操作(当然一次操作可以是crud组合)

  2. Session实例通过SessionFactory获取,用完需要关闭。

  3. Session是线程不同步的(不安全),因此要保证在同一线程中使用,可以用getCurrentSessiong()。

  4. Session可以看做是持久化管理器,它是与持久化操作相关的接口

2.示意代码:

Configuration cf=new Configuration().configure();
SessionFactory sf=cf.buildSessionFactory();
Session s=sf.getCurrentSession();
//或者是: Session s=sf.openSession();

Session(会话)接口的几个重要方法

save()、update()、saveOrUpdate():用于增加和修改对象
delete():用于删除对象
get()、load():根据主键查询
createQuery()、createSQLQuery():用于数据库操作对象
createCriteria():查询条件

get()和load()区别
  • get()方法直接返回实体类,如果查不到数据则返回null。load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用时,如果没有数据不存在,就会抛出个org.hibernate.ObjectNotFoundException异常

  • load先到缓存(session缓存/二级缓存)中去查,如果没有则返回一个代理对象(不马上到DB中去找),等后面使用这个代理对象操作的时候,才到DB中查询,这就是我们常说的 load在默认情况下支持延迟加载(lazy)

  • get先到缓存(session缓存/二级缓存)中去查,如果没有就到DB中去查(即马上发出sql)。总之,如果你确定DB中有这个对象就用load(),不确定就用get()(这样效率高)

load VS get
  • 如果查询不到数据,get 会返回 null,但是不会报错,load如果查询不到数据,则报错ObjectNotFoundException

  • 使用get 去查询数据,(先到一级/二级)会立即向db发出查询请求(select
    …),如果你使用的是load查询数据,(先到一级、二级))即使查询到对象,返回的是一个代理对象,如果后面没有使用查询结果,它不会真的向数据库发select,当程序员使用查询结果的时候才真的发出select,这个现象我们称为懒加载(lazy)

  • 通过修改配置文件(*.hbm.xml文件),我们可以取消懒加载

 <class lazy="false" name="User" table="t_user"></class>
  • 如何选择使用哪个: 如果你确定DB中有这个对象就用load(),不确定就用get()(这样效率高)

    openSession()和 getCurrentSession()区别

    1. 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
    2. 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭.
    3. 使用getCurrentSession()需要在hibernate.cfg.xml文件中加入

如下配置:
如果使用的是本地事务(jdbc事务)

<property name="hibernate.current_session_context_class">thread</property>

如果使用的是全局事务(jta事务)

<property name=” hibernate.transaction.factory_class”>
org.hibernate.transaction.JTATransactionFactory
</property>
openSession()和 getCurrentSession()联系

深入探讨:

  • 在 SessionFactory启动的时候,Hibernate 会根据配置创建相应的 CurrentSessionContext,在getCurrentSession()被调用的时候,实际被执行的方法是CurrentSessionContext.currentSession()。
  • 在currentSession()执行时,如果当前Session为空,currentSession会调用SessionFactory的openSession。
openSession()和 getCurrentSession()究竟选谁?

原则:
①如果需要在同一线程中,保证使用同一个Session则,使用getCurrentSession()
②如果在一个线程中,需要使用不同的Session,则使用opentSession()

openSession()和 getCurrentSession()联系,用ThreadLocal模式 (线程局部变量模式) 管理Session,代码如下:
public class HibernateUtil {
        public static final ThreadLocal session =new ThreadLocal();
        public static final SessionFactory sessionFactory;
        static {
            try {
                sessionFactory = new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                throw new ExceptionInInitializerError(ex);
            }
        }
        public static Session currentSession() throws HibernateException {
            Session s = session.get();
            if(s == null) {
                s = sessionFactory.openSession();session.set(s);
            }
            return s;
        }
        public static void closeSession() throws HibernateException {
            Session s = session.get();
            if(s != null) { 
                s.close();
            }
            session.set(null); 
        }
    }

如何确定你的session有没有及时关闭
window cmd netstat -an
[oracle 1521 mysql 3306 sql server 1433]
linux/unix netstat -anp top

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值