zoukankan      html  css  js  c++  java
  • Hibernate之全面认识

    Hibernate体系架构

    Hibernate通过配置文件管理底层的JDBC连接,将用户从原始的JDBC释放出来,使得用户无需再关注底层的JDBC操作,而是以面向对象的方式进行持久化操作。这种全面的解决方案架构如下(插图来自官方文档 manual:Comprehensive architecture)

    大致解释一下上面的关键分层模块

    SessionFactory: 是单个数据库映射关系经过编译后的内存镜像,是线程安全的。该对象可在进程或者集群的级别上,为事务直接可重用数据提供二级缓存。

    Session:是应用程序与持久层交互的一个单线程的对象,所有持久化的对象都必须在Session管理下才可以进行持久化操作。Session封装了JDBC,也是Transaction工厂。

    持久化对象:是一个与Session关联的普通对象(POJO)

    瞬态和脱管:未与Session关联的POJO处于瞬态。如果关联之后,Session如果关闭了,则此POJO转为脱管状态。

    ConnectionProvider:是生成JDBC连接的工厂。将应用程序与底层的DataSource及DriverManager隔离开。应用程序一般无需直接访问这个对象。

    TransactionFactory:生成Transaction对象的工厂。应用程序一般无需直接访问这个对象。

    Hibernate的配置文件

    Hibernate持久化操作离不开SessionFactory对象,而SessionFactory对象是由Configuration对象创建的(build)。每一个Hibernate配置文件对应一个Configuration对象,创建Configuration对象的方式有三种:

    1.使用hibernate.properties配置文件——这种方式不能在配置文件中直接添加映射文件,而是需要写到java code里面,所以比较麻烦

    在Hibernate发布包的projectetc下有一个hibernate.properties的用例,比较全面,部分配置如下,

     1 ## HypersonicSQL
     2 
     3 hibernate.dialect org.hibernate.dialect.HSQLDialect
     4 hibernate.connection.driver_class org.hsqldb.jdbcDriver
     5 hibernate.connection.username sa
     6 hibernate.connection.password
     7 hibernate.connection.url jdbc:hsqldb:./build/db/hsqldb/hibernate
     8 #hibernate.connection.url jdbc:hsqldb:hsql://localhost
     9 #hibernate.connection.url jdbc:hsqldb:test
    10 ....

    有了这个配置文件还不够,还需要在java code中写明映射文件名称,通过addResource("xxx.xml")方式可以添加映射文件,

    1 Configuration cfg = new Configuration()
    2 .addResource("a.hbm.xml")
    3 .addResource("b.hbm.xml");

    另外,也可以通过addClass("xxx.class")的方式直接添加实体类的Class实例,hibernate会自动搜索对应的映射文件,但是要求将映射文件和实体类放在同一个目录下。

    这种方式有个好处就是消除了系统对文件名的硬编码耦合。

    1 Configuration cfg = new Configuration()
    2 .addClass(hib.a.class)
    3 .addClass(hib.b.class);

    2.使用hibernate.cfg.xml配置文件

    即前面的例子,在hibernate.cfg.xml中配置hibernate连接信息,数据池等信息,另外还需要将映射文件名也添加进去,配置如下,

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
     3                                          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
     4 
     5 <hibernate-configuration>
     6     <session-factory>
     7         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
     8         <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property> <!-- 指定字符集的方式-->
     9         <property name="connection.username">root</property>
    10         <property name="connection.password"></property> 
    11 
    12         <property name="hibernate.c3p0.max_size">20</property>
    13         <property name="hibernate.c3p0.min_size">1</property>
    14     
    15         <property name="hibernate.c3p0.timeout">5000</property>
    16     
    17         <property name="hibernate.c3p0.max_statements">100</property>
    18         <property name="hibernate.c3p0.idle_test_period">3000</property>
    19         <property name="hibernate.c3p0.acquire_increment">2</property>
    20         <property name="hibernate.c3p0.validate">true</property>
    21         <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    22    
    23         <property name="hbm2ddl.auto">update</property>
    24         <property name="show_sql">true</property>
    25         <mapping resource="hib/News.hbm.xml" />
    26     </session-factory>
    27 </hibernate-configuration>
    View Code

     配置文件中,需要注意以下几项配置:

    1).编码:可以在url中指定,

    例如在hibernate.property中,setProperty("hibernate.connection.url","jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8")

    在hibernate.cfg.xml中, <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property> ,注意在xml文件中&符号要用 &amp;转义

    2).数据库方言hibernate.dialect,可以在官方手册中查找对应的选项

    3).自动建表 hibernate.hbm2ddl.auto有三种取值,update, create, create-drop

    4).hibernate.show_sql和hibernate.format_sql有助于调试sql,前者可以打印sql到控制台,后者进行格式化

    映射文件News.hbm.xml

     1 <?xml version="1.0"  encoding="UTF-8"?>    
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
     4 
     5 <hibernate-mapping package="hib">
     6     <class name="News" table="news_table">
     7         <id name="id" column="id" type="int">
     8             <generator class="identity" />
     9         </id>
    10         <property name="title" type="string" column="title" />
    11         <property name="content" type="string" column="content" />
    12     </class>
    13 </hibernate-mapping>
    View Code

    3.不使用任何配置文件,所有配置都以编码方式(.setProperty(key,value))来创建Configuration对象

     1 public static void noConfigFile() {
     2     Configuration cfg = new Configuration()
     3             .addClass(hib.News.class)
     4             .setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver")
     5             .setProperty("hibernate.connection.url","jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8")
     6             .setProperty("hibernate.connection.username", "root")
     7             .setProperty("hibernate.connection.password", "")
     8             .setProperty("hibernate.c3p0.max_size", "20")
     9             .setProperty("hibernate.c3p0.min_size", "1")
    10             .setProperty("hibernate.c3p0.max_statements", "100")
    11             .setProperty("hibernate.c3p0.timeout", "5000")
    12             .setProperty("hibernate.c3p0.idle_test_period", "3000")
    13             .setProperty("hibernate.c3p0.acquire_increment", "2")
    14             .setProperty("hibernate.validate", "true")
    15             .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
    16             .setProperty("hibernate.hbm2dll.auto", "create")
    17             .setProperty("hibernate.show_sql", "true");
    18     SessionFactory sf = cfg.buildSessionFactory();
    19     Session sess = sf.openSession();
    20     Transaction tx = sess.beginTransaction();
    21     News n = new News();
    22     n.setTitle("举头望明月");
    23     n.setContent("低头思故乡");
    24     sess.save(n);
    25     tx.commit();
    26     sess.close();
    27 }
  • 相关阅读:
    深拷贝呀,浅拷贝,再来一次复习整理
    移动端适配之路的一步步了解
    回文数
    整数反转
    关于DOM事件篇收集的知识点
    Html5新增的属性-querySelector
    Java常用的集合类
    VerifyCodeServlet(一次性验证码)
    EncodingFilter
    BaseServlet(一个Servlet多个处理方法)
  • 原文地址:https://www.cnblogs.com/fysola/p/6224629.html
Copyright © 2011-2022 走看看