先看基本框架图:
整体流程
1:通过configuration来读cfg.xml文件2:得到SessionFactory 工厂
3:通过SessionFactory 工厂来创建Session实例
4:通过Session打开事务
5:通过session的api操作数据库
6:事务提交
7:关闭连接
为什么要用:
1. 对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复性代码。
2. Hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现。他很大程度的简化DAO层的编码工作。
3. hibernate使用Java反射机制,而不是字节码增强程序来实现透明性。
4. hibernate的性能非常好,因为它是个轻量级框架。映射的灵活性很出色。它支持各种关系数据库,从一对一到多对多的各种复杂关系。
两种取得session方法:
openSession和getCurrentSession区别openSession:永远创建新的session 需手动close,事务提交不会自动close
getCurrentSession:事务没提交之前。在上下文找,有可能返回旧的session,事务提交了,会得到新的session。事务提交,session自动close
上下文,在配置文件指定:
<property name="current_session_context_class">thread</property>
jta | thread | managed | custom.Class
thread常用—》必须指明current_session_context_class,否则抛异常
a.sessionFactory.openSession();
- sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
- Session session = sessionFactory.openSession(); //创建新的session对象
- session.beginTransaction();
- session.save(t);
- session.getTransaction().commit();
- session.close() ; //关闭session对象
b.sessionFactory.getCurrentSession();
- sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
- Session session = sessionFactory.getCurrentSession(); //获取当前"上下文"中的session对象
- session.beginTransaction();
- session.save(t);
- Session session2 = sessionFactory.getCurrentSession();
- System.out.println(session == session2); //true
- session.getTransaction().commit(); //事务提交自动关闭该session
- System.out.println(session == session2); //false
两种使用hibernate的方法
不通过spring加载hibernate.cfg.xml,而是通过自定义的类HibernateUtil.java创建sessionFactory对象
- 1.TeamDaoHibernate
- package com.org.momo.dao.hibernateTemplate;
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import com.org.momo.bean.PageInfo;
- import com.org.momo.bean.Team;
- import com.org.momo.dao.TeamDao;
- import com.org.momo.util.HibernateUtil;
- public class TeamDaoHibernate implements TeamDao {
- private SessionFactory sessionFactory;
- public TeamDaoHibernatenotused() {
- sessionFactory = HibernateUtil.getSessionFactory();
- }
- public void insert(Team team) throws Exception {
- Session session = sessionFactory.openSession();
- Transaction transaction = session.beginTransaction();
- session.save(team);
- transaction.commit();
- session.close() ;
- }
- public void deleteById(Integer id) throws Exception {
- Session session = sessionFactory.openSession();
- Transaction transaction = session.beginTransaction();
- String hql = "delete from Team where id=?";
- Query query = session.createQuery(hql);
- query.setInteger(0, id);
- query.executeUpdate();
- transaction.commit();
- session.close();
- }
- public void update(Team team) throws Exception {
- Session session = sessionFactory.openSession();
- Transaction transaction = session.beginTransaction();
- session.merge(team);
- transaction.commit();
- session.close();
- }
- public List<Team> findAll() throws Exception {
- List<Team> teams = null;
- Session session = sessionFactory.openSession();
- Query query = session.createQuery("from Team");
- teams = query.list();
- session.close();
- return teams;
- }
- public Team findById(Integer id) throws Exception {
- Team team = null;
- Session session = sessionFactory.openSession();
- Query query = session.createQuery("from Team where id=?");
- query.setInteger(0, id);
- team = (Team)query.uniqueResult();
- session.close();
- return team;
- }
- public List<Team> findAllPage(PageInfo pageInfo) throws Exception {
- List<Team> teams = null;
- Session session = sessionFactory.openSession();
- Query queryTotal = session.createQuery("select count(id) from Team");
- int rowCount = ((Long)queryTotal.uniqueResult()).intValue() ; //先转换为Long 在转换为int
- int pageTotal = rowCount/pageInfo.getPageRows();
- if(rowCount%pageInfo.getPageRows() > 0) {
- pageTotal++;
- }
- pageInfo.setPageTotal(pageTotal);
- Query query = session.createQuery("from Team");
- query.setFirstResult(pageInfo.getPageRows()*(pageInfo.getCurrentPage()-1));
- query.setMaxResults(pageInfo.getPageRows());
- teams = query.list();
- session.close();
- return teams;
- }
- }
2.HibernateUtil.java
- package com.org.momo.util;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.AnnotationConfiguration;
- public class HibernateUtil {
- private static final SessionFactory sessionFactory;
- static{
- try{
- //初始化hibernate.cfg.xml配置,建立数据库连接
- sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
- }catch(Exception e){
- e.printStackTrace() ;
- throw new ExceptionInInitializerError(e) ;
- }
- }
- public static SessionFactory getSessionFactory(){
- return sessionFactory ;
- }
- }
通过配置Spring的applicationContext.xml文件加载,spring对Hibernate的支持(HibernateTemplate)
1.applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="teamService" class="com.org.momo.service.impl.TeamServiceImpl">
- <property name="teamDao" ref="teamDao"></property>
- </bean>
- <bean id="adminService" class="com.org.momo.service.impl.AdminServiceImpl">
- <property name="adminDao" ref="adminDao"></property>
- </bean>
- <bean id="logService" class="com.org.momo.service.impl.LogServiceImpl">
- <property name="logDao" ref="logDao"></property>
- </bean>
- <bean id="studentService" class="com.org.momo.service.impl.StudentServiceImpl">
- <property name="studentDao" ref="studentDao"></property>
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
- </bean>
- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <bean id="teamDao" class="com.org.momo.dao.hibernateTemplate.TeamDaoHibernateTemplate">
- <property name="hibernateTemplate" ref="hibernateTemplate"/>
- </bean>
- <bean id="adminDao" class="com.org.momo.dao.hibernateTemplate.AdminDaoHibernateTemplate">
- <property name="hibernateTemplate" ref="hibernateTemplate"/>
- </bean>
- <bean id="logDao" class="com.org.momo.dao.hibernateTemplate.LogDaoHibernateTemplate">
- <property name="hibernateTemplate" ref="hibernateTemplate"/>
- </bean>
- <bean id="studentDao" class="com.org.momo.dao.hibernateTemplate.StudentDaoHibernateTemplate">
- <property name="hibernateTemplate" ref="hibernateTemplate"/>
- </bean>
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
- </bean>
- </beans>
2.HibernateTemplate
- package com.org.momo.dao.hibernateTemplate;
- import java.sql.SQLException;
- import java.util.List;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.org.momo.bean.PageInfo;
- import com.org.momo.bean.Team;
- import com.org.momo.dao.TeamDao;
- public class TeamDaoHibernateTemplate extends HibernateDaoSupport implements TeamDao {
- public void deleteById(Integer id) throws Exception {
- Team team = this.getHibernateTemplate().load(Team.class, id) ;
- this.getHibernateTemplate().delete(team) ;
- }
- public List<Team> findAll() throws Exception {
- return this.getHibernateTemplate().loadAll(Team.class);
- }
- public Team findById(Integer id) throws Exception {
- List<Team> teams = this.getHibernateTemplate().find("from Team where id=?",id) ;
- if(!teams.isEmpty()){
- return teams.get(0) ;
- }else{
- return null;
- }
- }
- public void insert(Team team) throws Exception {
- this.getHibernateTemplate().save(team) ;
- }
- public void update(Team team) throws Exception {
- this.getHibernateTemplate().update(team);
- }
- public List<Team> findAllPage(final PageInfo pageInfo) throws Exception {
- List<Team> teams = null;
- int rowTotal = ((Long)this.getHibernateTemplate().find("select count(id) from Team").get(0)).intValue();
- int pageTotal = rowTotal/pageInfo.getPageRows();
- if(rowTotal%pageInfo.getPageRows() > 0) {
- pageTotal++;
- }
- pageInfo.setPageTotal(pageTotal);
- teams = this.getHibernateTemplate().executeFind(new HibernateCallback(){
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- Query query = session.createQuery("from Team");
- query.setFirstResult(pageInfo.getPageRows()*(pageInfo.getCurrentPage()-1));
- query.setMaxResults(pageInfo.getPageRows());
- return query.list();
- }
- });
- return teams;
- }
- }
Hibernate的查询方式
HQL : 是面向对象的查询语言,同SQL有些相似是Hib中最常用的方式。
查询设定各种查询条件。
支持投影查询,检索出对象的部分属性。
支持分页查询,允许使用having和group by
提供内制的聚集函数,sum(),min(),max()
能调用用户的自定义SQL
支持子查询,嵌入式查询
支持动态绑定参数
建议使用Query接口替换session的find方法。
Query Q = session.createQuery("from customer as c where c.name = :customerName" + "and c.age = :customerAge");
query.setString ("customerName" , "tom");
query.setInteger("customerAge" , "21");
list result = query.list();
QBC : QBCAPI提供了另一种方式,主要是Criteria接口、Criterion接口和Expression类
Criteria criteria = session.createCriteria(customer.class);Criterion criterion1 = Expression.like("name","t%");
Criterion criterion2 = Expression.eq("age",new Integer(21));
Critera = criteria.add(criterion1) ;
Critera = criteria.add(criterion2) ;
list result = criteria.list();
或是: list result = session.createCriteria(Customer.class).add(Expression.eq("this.name","tom")).list();
SQL : 采用HQL和QBC检索时,Hib生成SQL语句适用所有数据库。
Query query = session.createSQLQuery("select {c.*} from customers c where c.name like : customername " + "and c.age = :customerage","c",customer.calss);query.setString("customername","tom");
query.setInteger("customerage","21");
list result = query.list();
Hibernate 的缓存机制
详细见这里。
Hibernate缓存分类
一级缓存
又称为“Session的缓存”。Session内置不能被卸载,Session的缓存是事务范围的缓存(Session对象的生命周期通常对应一个数据库事务或者一个应用事务)。一级缓存中,持久化类的每个实例都具有唯一的OID。
二级缓存
关于二级缓存,可以看这里。
又称为“SessionFactory的缓存”。由于SessionFactory对象的生命周期和应用程序的整个过程对应,因此Hibernate二级缓存是进程范围或者集群范围的缓存,有可能出现并发问题,因此需要采用适当的并发访问策略,该策略为被缓存的数据提供了事务隔离级别。第二级缓存是可选的,是一个可配置的插件,默认下SessionFactory不会启用这个插件。
Hibernate提供了org.hibernate.cache.CacheProvider接口,它充当缓存插件与Hibernate之间的适配器。
第二级缓存使用
适用于:
1) 很少被修改的数据
2) 不是很重要的数据,允许出现偶尔并发的数据
3) 不会被并发访问的数据
4) 常量数据
不适用于:
1) 经常被修改的数据
2) 绝对不允许出现并发访问的数据,如财务数据,绝对不允许出现并发
3) 与其他应用共享的数据。
Session的延迟加载实现要解决两个问题
正常关闭连接和确保请求中访问的是同一个session。
Hibernate session就是java.sql.Connection的一层高级封装,一个session对应了一个Connection。http请求结束后正确的关闭session(过滤器实现了session的正常关闭);延迟加载必须保证是同一个session(session绑定在ThreadLocal)。
应用缓存
当Hibernate根据ID访问数据对象的时候,首先从Session一级缓存中查;
查不到,如果配置了二级缓存,那么从二级缓存中查;
如果都查不到,再查询数据库,把结果按照ID放入到缓存删除、更新、增加数据的时候,同时更新缓存。
一级缓存与二级缓存的对比
| 一级缓存 | 二级缓存 |
存放数据的形式 | 相互关联的持久化对象 | 对象的散装数据 |
缓存的范围 | 事务范围,每个事务都拥有单独的一级缓存 | 进程范围或集群范围,缓存被同一个进程或集群范围内所有事务共享 |
并发访问策略 | 由于每个事务都拥有单独的一级缓存不会出现并发问题,因此无须提供并发访问策略 | 由于多个事务会同时访问二级缓存中的相同数据,因此必须提供适当的并发访问策略,来保证特定的事务隔离级别 |
数据过期策略 | 处于一级缓存中的对象永远不会过期,除非应用程序显示清空或者清空特定对象 | 必须提供数据过期策略,如基于内存的缓存中对象的最大数目,允许对象处于缓存中的最长时间,以及允许对象处于缓存中的最长空闲时间 |
物理介质 | 内存 | 内存和硬盘,对象的散装数据首先存放到基于内存的缓存中,当内存中对象的数目达到数据过期策略的maxElementsInMemory值,就会把其余的对象写入基于硬盘的缓存中 |
缓存软件实现 | 在Hibernate的Session的实现中包含 | 由第三方提供,Hibernate仅提供了缓存适配器,用于把特定的缓存插件集成到Hibernate中 |
启用缓存的方式 | 只要通过Session接口来执行保存,更新,删除,加载,查询,Hibernate就会启用一级缓存,对于批量操作,如不希望启用一级缓存,直接通过JDBCAPI来执行 | 用户可以再单个类或类的单个集合的粒度上配置第二级缓存,如果类的实例被经常读,但很少被修改,就可以考虑使用二级缓存,只有为某个类或集合配置了二级缓存,Hibernate在运行时才会把它的实例加入到二级缓存中 |
用户管理缓存的方式 | 一级缓存的物理介质为内存,由于内存的容量有限,必须通过恰当的检索策略和检索方式来限制加载对象的数目,Session的evit()方法可以显示的清空缓存中特定对象,但不推荐 | 二级缓存的物理介质可以使内存和硬盘,因此第二级缓存可以存放大容量的数据,数据过期策略的maxElementsInMemory属性可以控制内存中的对象数目,管理二级缓存主要包括两个方面:选择需要使用第二级缓存的持久化类,设置合适的并发访问策略;选择缓存适配器,设置合适的数据过期策略。SessionFactory的evit()方法也可以显示的清空缓存中特定对象,但不推荐 |
缓存管理方法
一级缓存的管理
evit(Object obj) 将指定的持久化对象从一级缓存中清除,释放对象所占用的内存资源,指定对象从持久化状态变为脱管状态,从而成为游离对象。
clear() 将一级缓存中的所有持久化对象清除,释放其占用的内存资源。
contains(Object obj) 判断指定的对象是否存在于一级缓存中。
flush() 刷新一级缓存区的内容,使之与数据库数据保持同步。
当调用session的save()方法。当session对象调用save()方法保存一个对象后,该对象会被放入到session的缓存中。 get()和load()。当session对象调用get()或load()方法从数据库取出一个对象后,该对象也会被放入到session的缓存中。 使用HQL和QBC等从数据库中查询数据。
public class Client
{
public static void main(String[] args)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try
{
/*开启一个事务*/
tx = session.beginTransaction();
/*从数据库中获取id="402881e534fa5a440134fa5a45340002"的Customer对象*/
Customer customer1 = (Customer)session.get(Customer.class, "402881e534fa5a440134fa5a45340002");
System.out.println("customer.getUsername is"+customer1.getUsername());
/*事务提交*/
tx.commit();
System.out.println("-------------------------------------");
/*开启一个新事务*/
tx = session.beginTransaction();
/*从数据库中获取id="402881e534fa5a440134fa5a45340002"的Customer对象*/
Customer customer2 = (Customer)session.get(Customer.class, "402881e534fa5a440134fa5a45340002");
System.out.println("customer2.getUsername is"+customer2.getUsername());
/*事务提交*/
tx.commit();
System.out.println("-------------------------------------");
/*比较两个get()方法获取的对象是否是同一个对象*/
System.out.println("customer1 == customer2 result is "+(customer1==customer2));
}
catch (Exception e)
{
if(tx!=null)
{
tx.rollback();
}
}
finally
{
session.close();
}
}
}
结果
Hibernate:
select
customer0_.id as id0_0_,
customer0_.username as username0_0_,
customer0_.balance as balance0_0_
from
customer customer0_
where
customer0_.id=?
customer.getUsername islisi
-------------------------------------
customer2.getUsername islisi
-------------------------------------
customer1 == customer2 result is true
输出结果中只包含了一条SELECT SQL语句,而且customer1 == customer2 result is true说明两个取出来的对象是同一个对象。其原理是:第一次调用get()方法, Hibernate先检索缓存中是否有该查找对象,发现没有,Hibernate发送SELECT语句到数据库中取出相应的对象,然后将该对象放入缓存中,以便下次使用,第二次调用get()方法,Hibernate先检索缓存中是否有该查找对象,发现正好有该查找对象,就从缓存中取出来,不再去数据库中检索。
二级缓存的管理:
evict(Class arg0, Serializable arg1)将某个类的指定ID的持久化对象从二级缓存中清除,释放对象所占用的资源。
sessionFactory.evict(Customer.class, new Integer(1));
evict(Class arg0) 将指定类的所有持久化对象从二级缓存中清除,释放其占用的内存资源。
sessionFactory.evict(Customer.class);
evictCollection(String arg0) 将指定类的所有持久化对象的指定集合从二级缓存中清除,释放其占用的内存资源。
sessionFactory.evictCollection("Customer.orders");
常用的二级缓存插件
EHCache org.hibernate.cache.EhCacheProvider
OSCache org.hibernate.cache.OSCacheProvider
SwarmCahe org.hibernate.cache.SwarmCacheProvider
JBossCache org.hibernate.cache.TreeCacheProvider
<!-- EHCache的配置,hibernate.cfg.xml -->
<hibernate-configuration>
<session-factory>
<!-- 设置二级缓存插件EHCache的Provider类-->
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<!-- 启动"查询缓存" -->
<property name="hibernate.cache.use_query_cache">
true
</property>
</session-factory>
</hibernate-configuration>
<!-- ehcache.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--
缓存到硬盘的路径
-->
<diskStore path="d:/ehcache"></diskStore>
<!--
默认设置
maxElementsInMemory : 在內存中最大緩存的对象数量。
eternal : 缓存的对象是否永远不变。
timeToIdleSeconds :可以操作对象的时间。
timeToLiveSeconds :缓存中对象的生命周期,时间到后查询数据会从数据库中读取。
overflowToDisk :内存满了,是否要缓存到硬盘。
-->
<defaultCache maxElementsInMemory="200" eternal="false"
timeToIdleSeconds="50" timeToLiveSeconds="60" overflowToDisk="true"></defaultCache>
<!--
指定缓存的对象。
下面出现的的属性覆盖上面出现的,没出现的继承上面的。
-->
<cache name="com.suxiaolei.hibernate.pojos.Order" maxElementsInMemory="200" eternal="false"
timeToIdleSeconds="50" timeToLiveSeconds="60" overflowToDisk="true"></cache>
</ehcache>
<!-- *.hbm.xml -->
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class>
<!-- 设置该持久化类的二级缓存并发访问策略 read-only read-write nonstrict-read-write transactional-->
<cache usage="read-write"/>
</class>
</hibernate-mapping>
若存在一对多的关系,想要在在获取一方的时候将关联的多方缓存起来,需要在集合属性下添加<cache>子标签,这里需要将关联的对象的hbm文件中必须在存在<class>标签下也添加<cache>标签,不然Hibernate只会缓存OID。
<hibernate-mapping>
<class name="com.suxiaolei.hibernate.pojos.Customer" table="customer">
<!-- 主键设置 -->
<id name="id" type="string">
<column name="id"></column>
<generator class="uuid"></generator>
</id>
<!-- 属性设置 -->
<property name="username" column="username" type="string"></property>
<property name="balance" column="balance" type="integer"></property>
<set name="orders" inverse="true" cascade="all" lazy="false" fetch="join">
<cache usage="read-only"/>
<key column="customer_id" ></key>
<one-to-many class="com.suxiaolei.hibernate.pojos.Order"/>
</set>
</class>
</hibernate-mapping>