不同版本Hibernate.获取SessionFactory的方式

Hibernate 版本说明:

  我当前使用的是 Hibernate 5.x ,(hibernate-release-5.3.6.Final.zip),从官网下载的。解压zip压缩包,包中有一个文件夹是:required ,将其下的所有jar包全部导入到工程中。并添加mysql-connector-xxx.jar包。

  hibernate-release-5.3.6.Final/lib/required/中的jar包

  

  项目工程中的lib

  

  HibernateUtil.java

 package com.charles.hibernate.util;

 import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; /**
* <p>Type: HibernateUtil</p>
* <p>Description: Hibernate工具类.</p>
* @author baitang.<gy03554>
* @date 2018年10月16日 上午12:37:58
* @version v1.0.0
*/
public class HibernateUtil { // 配置文件的位置
private static final String CONFIGURE_XML = "hibernate.cfg.xml"; /**
* Hibernate 3.x 获取SessionFactory方式.
* @return SessionFactory
*/
private static SessionFactory buildHibernate3SessionFactory() { // 1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息
Configuration configuration = new Configuration().configure(CONFIGURE_XML); return configuration.buildSessionFactory();
} /**
* Hibernate 4.x 获取SessionFactory方式
* @return SessionFactory
*/
private static SessionFactory buildHibernate4SessionFactory() { // 1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息
Configuration configuration = new Configuration().configure(CONFIGURE_XML); // 2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象
// hibernate 的任何配置和服务都需要在该对象中注册后才能有效.
// ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
// .buildServiceRegistry();
// return configuration.buildSessionFactory(serviceRegistry); return configuration.buildSessionFactory(); } /**
* Hibernate 5.x 获取SessionFactory方式
* @return SessionFactory
*/
private static SessionFactory buildHibernate5SessionFactory() { // A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure(CONFIGURE_XML) // configures settings from hibernate.cfg.xml
.build();
try {
return new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
return null;
} /**
* 获取SessionFactory方法
* @param sessionVersion Hibernate的版本号(取值为:3,4,5)
* @return SessionFactory
*/
public synchronized static SessionFactory getSessionFactory(int sessionVersion) { if (3 == sessionVersion) { return buildHibernate3SessionFactory();
} else if (4 == sessionVersion) { return buildHibernate4SessionFactory();
} else if (5 == sessionVersion) { return buildHibernate5SessionFactory();
}
return null;
} }

如有问题,欢迎纠正!!!

如有转载,请标明源处:https://www.cnblogs.com/Charles-Yuan/p/9795688.html

不同版本Hibernate.获取SessionFactory的方式的更多相关文章

  1. 4.0之后的hibernate获取sessionFactory

    static{ Configuration config=new Configuration().configure(); ServiceRegistry resgistry = new Servic ...

  2. Hibernate中两种获取Session的方式

    转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...

  3. Hibernate操作和保存方式

    Session API [Java Hibernate 之 CRUD 操作]http://www.codeceo.com/article/java-hibernate-crud.html   [Ses ...

  4. Spring 集成Hibernate的三种方式

    首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件 ...

  5. Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能

    由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...

  6. 从零开始学 Web 之 DOM(二)对样式的操作,获取元素的方式

    大家好,这里是「 Daotin的梦呓 」从零开始学 Web 系列教程.此文首发于「 Daotin的梦呓 」公众号,欢迎大家订阅关注.在这里我会从 Web 前端零基础开始,一步步学习 Web 相关的知识 ...

  7. 8 -- 深入使用Spring -- 8...2 管理Hibernate的SessionFactory

    8.8.2 管理Hibernate的SessionFactory 当通过Hibernate进行持久层访问时,必须先获得SessionFactory对象,它是单个数据库映射关系编译后的内存镜像.在大部分 ...

  8. 十八、springboot中hibernate配置sessionFactory访问数据库

    前提 在yml或properties文件中配置数据库与数据库连接池 Hibernate配置 几种方式: 方式一: @Configuration public class HibernateConfig ...

  9. spring与hibernate注解及XML方式集成

    spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ...

随机推荐

  1. MVC 实用架构设计(三)——EF-Code First(5):二级缓存

    一.前言 今天我们来谈谈EF的缓存问题. 缓存对于一个系统来说至关重要,但是是EF到版本6了仍然没有见到有支持查询结果缓存机制的迹象.EF4开始会把查询语句编译成存储过程缓存在Sql Server中, ...

  2. URL地址中的#

    1.#的涵义 代表网页中的一个位置.井号后面的字符,就是该位置的标识符.比如, http://www.baidu.com/index.html#one 就代表网页index.html的one位置.浏览 ...

  3. HZAU2018年十大阅读之星演讲稿

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  4. odoo 权限设置

    *权限管理的四个层次    # 菜单级别:不属于指定菜单所包含组的用户看不到该菜单,不客全,只是隐藏                 菜单,若知道菜单ID,仍然可以通过指定URL访问    # 对象级 ...

  5. Struts2漏洞检查工具

  6. js阻止默认事件,如a标签跳转和事件冒泡

    禁止a标签点击跳转 <a href="http://baidu.com" onclick="return false">点我啊</a> ...

  7. 28-1-LTDC显示中英文

    1.字符编码 由于计算机只能识别 0 和 1,文字也只能以 0 和 1 的形式在计算机里存储,所以我们需要对文字进行编码才能让计算机处理,编码的过程就是规定特定的 01 数字串表示特定的文字,最简单的 ...

  8. java实现消息队列的两种方式

    https://blog.csdn.net/fenfenguai/article/details/79257928

  9. python练习题-day5

    1.有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...

  10. dos2unix命令

    dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter).DOS下的文本文件是以\r\n作为断行标志的 ...