Hibernate介绍, 语法 和 Hibernate的基本元素

1.   传统的JDBC操作

获取连接;

创建可执行对象;

执行数据库操作;

2.   JDBC连接的工具化, JDBC操作的工具化

用公共类实现数据库的连接, 查询, 新增,更新,删除以及关闭连接操作.

3.   JDBC连接的配置化

把连接信息配置在文本文件中,这样修改环境不需要重新编译java文件

把数据库操作封装在工具类中,业务不需要关心数据库连接环境。(同第二种方法)。

4.   连接池的引入

对操作结束后的连接不马上关闭, 而是维持一个合理的空闲连接数, 获取连接也不一定完全新建连接, 可以从空闲连接池中获取.

5.   POJO, 数据的对象化

POJO 在 Hibernate 语义中理解为数据库表所对应的 Domain  Object。这里的 POJO 就是所谓的“Plain  Ordinary  Java  Object"或者"Plain Old Java Object”""",字面上来讲就是无格式普通 Java  对象,简 单的可以理解为一个不包含逻辑代码的值对象(Value Object  简称 VO)。

一个典型的 POJO:

public class TUser implements Serializable {

private String name;

 

public User(String name) {

this.name = name;

}

 

public User() {

}

 

public String getName() {

return this.name;

}

 

public void setName(String name) {

this.name = name;

}

....

}

6.   持久层的对象化, 持久层的框架化

数据操作不局限于表,列的形式,  而可以把表映射为类的对象,

对数据库连接池, 会话的管理实行统一的框架化;

7.   Hibernate是什么?

2001年末, Hibernate第一个正式版本发布; 作者为Gavin King

2003年6月8日, Hibernate 2发布; 2003年末被JBoss收购.

2005年3月, Hibernate 3 发布.

8.   Hibernate的jar, 软件环境

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上.

原文地址:https://www.cnblogs.com/renyuan/p/2686909.html