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发布包的\project\etc下有一个hibernate.properties的用例,比较全面,部分配置如下,

 ## HypersonicSQL

 hibernate.dialect org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:./build/db/hsqldb/hibernate
#hibernate.connection.url jdbc:hsqldb:hsql://localhost
#hibernate.connection.url jdbc:hsqldb:test
....

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

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

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

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

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

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

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

 <?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"> <hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property> <!-- 指定字符集的方式-->
<property name="connection.username">root</property>
<property name="connection.password"></property> <property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property> <property name="hibernate.c3p0.timeout">5000</property> <property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="hib/News.hbm.xml" />
</session-factory>
</hibernate-configuration>

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

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

 <?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 package="hib">
<class name="News" table="news_table">
<id name="id" column="id" type="int">
<generator class="identity" />
</id>
<property name="title" type="string" column="title" />
<property name="content" type="string" column="content" />
</class>
</hibernate-mapping>

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

 public static void noConfigFile() {
Configuration cfg = new Configuration()
.addClass(hib.News.class)
.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url","jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8")
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.c3p0.max_size", "20")
.setProperty("hibernate.c3p0.min_size", "1")
.setProperty("hibernate.c3p0.max_statements", "100")
.setProperty("hibernate.c3p0.timeout", "5000")
.setProperty("hibernate.c3p0.idle_test_period", "3000")
.setProperty("hibernate.c3p0.acquire_increment", "2")
.setProperty("hibernate.validate", "true")
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.hbm2dll.auto", "create")
.setProperty("hibernate.show_sql", "true");
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
News n = new News();
n.setTitle("举头望明月");
n.setContent("低头思故乡");
sess.save(n);
tx.commit();
sess.close();
}

Hibernate之全面认识的更多相关文章

  1. hibernate多对多关联映射

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  2. 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

  3. hibernate多对一双向关联

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  4. Hibernate中事务的隔离级别设置

    Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下

  5. Hibernate中事务声明

    Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...

  6. spring applicationContext.xml和hibernate.cfg.xml设置

    applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  7. [原创]关于Hibernate中的级联操作以及懒加载

    Hibernate: 级联操作 一.简单的介绍 cascade和inverse (Employee – Department) Casade用来说明当对主对象进行某种操作时是否对其关联的从对象也作类似 ...

  8. hibernate的基本xml文件配置

    需要导入基本的包hibernate下的bin下的required和同bin下optional里的c3p0包下的所有jar文件,当然要导入mysql的驱动包了.下面需要注意的是hibernate的版本就 ...

  9. Maven搭建SpringMVC+Hibernate项目详解 【转】

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

  10. 1.Hibernate简介

    1.框架简介: 定义:基于java语言开发的一套ORM框架: 优点:a.方便开发;           b.大大减少代码量;           c.性能稍高(不能与数据库高手相比,较一般数据库使用者 ...

随机推荐

  1. js的forEach,for in , for of

    forEach遍历数组 [].forEach(function(value, index, array) { // ... }); 例子 var myArry =[1,2,3,4]; myArry.d ...

  2. iOS导航栏的正确隐藏方式【转】

    简介:在项目中经常碰到首页顶部是无限轮播,需要靠最上面显示.有的设置导航栏为透明等一系列的方法,这个可以借助第三方.或者干脆简单粗暴的直接隐藏掉导航栏.可是push到下一个页面的时候是需要导航栏的,如 ...

  3. 高级java必会系列二:多线程经常使用的3个关键字:synchronized、ReentrantLock、volatile

    系列一讲解了多线程,本章讲解多线程开发中经常使用到的3个关键字synchronized.ReentrantLock.volatile. 一.synchronized 互斥锁,即操作互斥,并发线程过来, ...

  4. jQuery 跨域访问的三种方式 No 'Access-Control-Allow-Origin' header is present on the reque

    问题: XMLHttpRequest cannot load http://v.xxx.com. No 'Access-Control-Allow-Origin' header is present ...

  5. ARM 开发工具 Keil和DS-5的比较。

    http://www.eeboard.com/bbs/thread-25219-1-1.html 如今ARM体系架构的处理器在嵌入式市场上呼风唤雨,从低端的MCU应用到高端的多媒体消费电子,移动设备领 ...

  6. python爬虫感想

    老师说,能用程序解决一个实际问题,说明已经会使用工具了.今天用python做了一个小爬虫,很幸运,成功了. 有几个难点:就是学会伪装,还有一个觉得打开的方式太多,有点糊涂,正则表达式也要加强了.

  7. SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  8. opencv,图片遍历

    //肤色提取,skinArea为二值化肤色图像 void skinExtract(const Mat &frame, Mat &skinArea) { Mat YCbCr; vecto ...

  9. Android 自定义view (一)——attr 理解

    前言: 自定义view是android自定义控件的核心之一,那么在学习自定义view之前,我们先来了解下自定义view的自定义属性的attr的用法吧 Android attr 是什么 (1)attr ...

  10. gulp 安装 使用 和删除

    1.安装 全局安装: npm intstall gulp -g      (首先你得有node.js ,这个可以去node 官网下载个iso的镜像安装包,傻瓜式安装.自带npm) 安装在项目中: 首先 ...