zoukankan      html  css  js  c++  java
  • 二、Hibernate的基本元素

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

  • 相关阅读:
    三十五、常用控件
    三十九.导入工程出错
    三十七、创建无图标的应用
    三十二、汉字排序
    三十八、分辨率适配方法
    四十一、打开各种文件的intent
    ExamTime
    MusicPXY3.1
    单目运算符的最新认识
    寄快递费用
  • 原文地址:https://www.cnblogs.com/linzheng/p/1924850.html
Copyright © 2011-2022 走看看