不同版本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. virtuanenv+flask

    1.virtualenv&flask 专门为特定项目创建一个目录和一个虚拟的Python 运行环境 # 1.安装 virtualenv$ pip3 install virtualenv #.创 ...

  2. vsftpd上传文件出现553 Could not create file错误解决方法

    1.确定目录权限 2.关闭selinux

  3. 确界原理 supremum and infimum principle 戴德金定理 Dedekind theorem

    确界原理  supremum and infimum principle  戴德金定理  Dedekind theorem http://www.math.ubc.ca/~cass/courses/m ...

  4. 2016年蓝桥杯省赛A组c++第5题(计算机组成原理)

    /* 下面的代码把一个整数的二进制表示的最右边的连续的1全部变成0 如果最后一位是0,则原数字保持不变. 如果采用代码中的测试数据,应该输出: 0000000000000000000000000110 ...

  5. opencv -python

    https://www.python----------tab.com/html/2017/pythonhexinbiancheng_1120/1184.html http://www.cnblogs ...

  6. iOS-静态库,动态库,framework,bundle浅析(四)

    1. 创建bundle,如图,点击 +  ,弹出选择框, macOS 下的Framework & Library  ,点击bundle,输入bundle的名字,然后点击 finish.   图 ...

  7. 读书笔记iOS-Core-Animation-Advanced-Techniques,iOS性能调试工具

    调试卡顿,除了使用timer profile,还可以使用 OpenGL ES驱动工具 OpenGL ES Driver工具显示的GPU利用率,打开Color Blended Layers 我们给图片和 ...

  8. 20165225 2017-2018-2《Java程序设计》课程总结

    20165225 2017-2018-2<Java程序设计>课程总结 - 每周作业链接汇总: 预备作业一:我期待的师生关系 预备作业二:学习基础和C语言基础调查 预备作业三:linux安装 ...

  9. Jenkins tomcat打包启动脚本,待完善

    shell脚本 #!/bin/bashJENKINS_HOME=/usr/local/jenkinsTOMCAT_HOME=/usr/local/jenkins/tomcat-testSHUTDOWN ...

  10. caffe 测试时间报错 Aborted at unix time

    今天测试时间报错,具体如下图: 在网上查了一下,大概的原因是由于程序中使用了随机函数造成的,后来检查了一下prototxt中有可能含有随机数的地方,去掉之后就可以了,包括shuffle:true,以及 ...