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. python批量进行文件修改操作

    python批量修改文件扩展名 在网上下载了一些文件,因为某种原因,扩展名多了一个后缀'.xxx',手动修改的话因为文件太多,改起来费时费力,于是决定写个小脚本进行修改. 1.要点: import r ...

  2. 对于似1/(1+x^4)型的不定积分的总结

    最近在求解一道不定积分的经典例题时遇到了一点小麻烦.的确,在处理1/(1+x^4)积分的时候,需要一定的技巧性,不然会使计算量变得庞大. 下面,我简单的总结了类似结构不定积分的求解方法,希望大家看完之 ...

  3. jquery的$().each,$.each的区别

    在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...

  4. java 基础二 Graphics类

    一.处理图形 1.画直线 void drawLine (int startx , int starty , int endx , int endy) 参数列表:直线开始的横坐标.纵坐标,直线结束的横坐 ...

  5. MVC+knocKout.js 实现下拉框级联

    数据库:部门表和员工表 在控制器里面的操作: public ActionResult Index3() { ViewBag.departments = new SelectList(getDepart ...

  6. Android新组件CardView

    Android L以后,新增了一个CardView组件,Google官方应用中有不少地方是使用卡片来展示信息,背后应该就是这个CardView. 使用CardView要引入单独的support包:co ...

  7. python_编程规范

    缩进 4个"空格"作为一个缩进层次,永远不要使用"制表位" 空格 运算符两边放置一个空格 命名  模块名:模块应该是不含下画线的.简短的.小写的名字.  类名: ...

  8. python---基础之模块,列表,元组,字典

    1. 模块 写模块的时候尽量不要和系统自带的模块的名字相同 调用模块的时候,会先在当前目录下查找是否有这个模块,然后再会如python的环境变量中查找   a.模块1:sys 代码如下: import ...

  9. delphi 环境问题

    这个编译时的警告该如何理解?[Warning] Unit 'Unit101' implicitly imported into package 'Package202'------解决方案------ ...

  10. [Python] 删除指定目录下后缀为 xxx 的过期文件

    import os import time import datetime def should_remove(path, pattern, days): if not path.endswith(p ...