博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二、Hibernate的基本元素
阅读量:6709 次
发布时间:2019-06-25

本文共 5082 字,大约阅读时间需要 16 分钟。

一. Hibernate的基本元素

Configuration---hibernate.cfg.xml

1. hibernate.cfg.xml 或 hibernate.properties 默认的配置文件

只需选择一种形式的配置方式, properties形式的文件不配置mapping子节点,且不使用xml的格式:

一个典型的xml配置文件如下:

 

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<!-- Generated by MyEclipse Hibernate Tools. -->

<hibernate-configuration>

<session-factory>

<property name="connection.username">root</property>

<property name="connection.url">

jdbc:mysql://localhost:3306/test

</property>

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="connection.password">root</property>

<property name="connection.driver_class">

org.gjt.mm.mysql.Driver

</property>

<property name="show_sql">true</property>

<property name="hibernate.hbm2ddl.auto">create</property>

<mapping resource="cn/thinkmore/hibernate/pojo/tuser.hbm.xml" />

<!—

other mapping element...

-->

</session-factory>

</hibernate-configuration>

 

<?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 name="hibernate3.pojo.Tuser" table="t_user">

<id column="ID" name="id" type="java.lang.String">

<generator class="uuid.hex" />

</id>

<property column="NAME" name="name" type="java.lang.String" />

<property column="EMAIL" name="email" type="java.lang.String" />

</class>

</hibernate-mapping>

 

对应的hibernate.properties文件为:

connection.username root

connection.url jdbc:mysql://localhost:3306/test

dialect org.hibernate.dialect.MySQLDialect

connection.password root

connection.driver_class org.gjt.mm.mysql.Driver

show_sql true

2. 配置文件-----数据库连接配置

可以选择JDBC配置或者JNDI中的一种:

JDBC配置项:

dialect ---- 数据库适配器, 每个数据库略有不同

connection.driver_class --- 数据库驱动类

connection.url --- 数据库URL

connection.username --- 数据库用户名

connection.password --- 数据库登陆密码(对应的)

 

JNDI配置项:

dialect ---- 数据库适配器, 同上

connection.datasource ---数据库JNDI名称

connection.username --- 数据库用户名, 同上

connection.password --- 数据库登陆密码(对应的) , 同上

3. 配置文件-----数据库连接池配置

目前Hibernate支持的4种连接池组件, 除了Hibernate默认的都需要指出hibernate.connection.provider_class.

 

hibernate默认的(hibernate.properties文件为例):

hibernate.connection.pool_size 2

 

 

C3P0

 

 

Dbcp(推荐)

 

 

Proxool

4. Transaction(事务管理)

hibernate.transaction.factory_class配置Transaction实例工厂类二选一.

 

JDBC的事务处理机制:

hibernate.transaction.factory_class org.hibernate.transaction. JDBCTransaction

 

使用JTA

hibernate.transaction.factory_class org.hibernate.transaction. JTATransaction

jta.UserTransaction jta/usertransaction

 

配置, Session相关

5. Configuration类(org.hibernate.cfg.Configuration类)

为了获取SessionFactory, 一般只需执行一次. xml形式的配置文件比较方便:

 

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

也可以指定一个文件进行加载,而不使用默认的hibernate.cfg.xml文件,

java.io.File file = new java.io.File(“…..”);

Configuration config = new Configuration().configure(file);

对应properties形式的配置方式必须手工加载映射文件,比如:

Configuration config=new Configuration();

config.addClass(TUser.class);

6. SessionFactory(org.hibernate.SessionFactory接口)

SessionFactory顾名思义, 就是session的工厂类. 创建SessionFactory的实例就是调用已经装载了配置信息的Configuration对象的buildSessionFactory()方法:

 

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

SessionFactory sessionFactory = config.buildSessionFactory();

 

Hibernate2中buildSessionFactory()方法声明了抛出异常.

SessionFactory对象中保存了配置信息. 如果要使用多个数据库, 需要针对每个数据库分别建立对应的SessionFactory实例, 如果需要动态改变config文件, 则需要动态重建SessionFactory的实例.

SessionFactory中还保存了二级数据缓存和Statement Pool, 它是线程安全的, 所以一个数据库的SessionFactory一般作为单实例存在.

7. 得到Session, Session的线程局部化

Session newSession = sessionFactory.openSession();

Session代表Hibernate的会话, 作用就像JDBC的Conection对象. Hibernate2的find方法已经被废除.

Session是非线程安全的, 所以不应该对同一个Session实例进行多线程同时调用.

 

HibernateUtil和静态SessionFactory一起工作, 由ThreadLocal管理Hibernate Session。通过ThreadLocal类型来实现Session的线程独立. ThreadLocal在JVM内部维护一个Map, key是当前的线程对象, value是在当前线程中调用ThreadLocal对象的set方法时传递的参数. 当调用ThreadLocal对象的get方法时, ThreadLocal对象会把当前线程对象的引用作为key从Map中取出绑定的对象.

package cn.thinkmore.hibernate.session;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class HibernateUtil{

private static SessionFactory sessionFactory;

static {

sessionFactory = new Configuration().configure().buildSessionFactory();

}

 

private static final ThreadLocal<Session> SESSIONCONTAINER = new ThreadLocal<Session>();

 

public static Session currentSession() {

Session se = (Session) SESSIONCONTAINER.get();

if (null == se) {

se = sessionFactory.openSession();

SESSIONCONTAINER.set(se);

}

return se;

}

 

public static void closeSession() {

Session se = (Session) SESSIONCONTAINER.get();

if (null == se) {

// SESSIONCONTAINER.set(null);

else {

se.close();

}

}

}

在WebApplication中为了更好的管理Session的生命周期, 可以把静态ThreadLocal对象定义在过滤器中, 在进入过滤器时调用其set(新的Session); 执行doFilter后, 在调用get方法去除Session并进行关闭.在业务操作中,统一用过滤器的静态ThreadLocal获取Session, 就保证了每一个request对象只能使用一个独立的Session.

由于一些EJB可能会运行在同一个事务但不同线程的环境中, 所以这个方法不能照搬到EJB环境中.建议在托管环境中,将SessionFactory绑定到JNDI上.

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080833

转载地址:http://jsslo.baihongyu.com/

你可能感兴趣的文章
手机端网页web开发要点
查看>>
Eclipse开发工具的使用之-使用Eclipse的Debug调试Android程序
查看>>
小弟浅谈asp.net页面生成周期---下
查看>>
正则表达式中 group groups区别
查看>>
JBoss + EJB3 + MySql : 开发第一个EJB
查看>>
浏览器请求阻塞到底是怎么回事?我们为什么要把静态资源分服务器放置?
查看>>
Oracle数据库基础知识
查看>>
2011年9月最新整理的10个有趣的jQuery插件集合
查看>>
Python的日志配置和处理
查看>>
小程序设置全屏显示
查看>>
c++ bind的简单使用 实例
查看>>
(翻译)Angular 1.3中的验证器管道
查看>>
Web网站的性能测试工具
查看>>
【linux+C】通过几个实例温习指针
查看>>
I.MX6 Manufacturing Tool V2 (MFGTool2) Emmc mksdcard.sh hacking
查看>>
异步复位同步释放
查看>>
螺旋折线——第九届蓝桥杯C语言B组(省赛)第七题
查看>>
木马另类删除文件的方法
查看>>
数学四则运算
查看>>
HDU 1241 Oil Deposits
查看>>