不同版本Hibernate.获取SessionFactory的方式
不同版本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的方式的更多相关文章
- 4.0之后的hibernate获取sessionFactory
static{ Configuration config=new Configuration().configure(); ServiceRegistry resgistry = new Servic ...
- Hibernate中两种获取Session的方式
转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...
- Hibernate操作和保存方式
Session API [Java Hibernate 之 CRUD 操作]http://www.codeceo.com/article/java-hibernate-crud.html [Ses ...
- Spring 集成Hibernate的三种方式
首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件 ...
- Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能
由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...
- 从零开始学 Web 之 DOM(二)对样式的操作,获取元素的方式
大家好,这里是「 Daotin的梦呓 」从零开始学 Web 系列教程.此文首发于「 Daotin的梦呓 」公众号,欢迎大家订阅关注.在这里我会从 Web 前端零基础开始,一步步学习 Web 相关的知识 ...
- 8 -- 深入使用Spring -- 8...2 管理Hibernate的SessionFactory
8.8.2 管理Hibernate的SessionFactory 当通过Hibernate进行持久层访问时,必须先获得SessionFactory对象,它是单个数据库映射关系编译后的内存镜像.在大部分 ...
- 十八、springboot中hibernate配置sessionFactory访问数据库
前提 在yml或properties文件中配置数据库与数据库连接池 Hibernate配置 几种方式: 方式一: @Configuration public class HibernateConfig ...
- spring与hibernate注解及XML方式集成
spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ...
随机推荐
- OLW Test
第一篇使用Open Live Writer 发布测试. Code Insert: #include <stdio.h> main() { printf("Hello World! ...
- 2015年蓝桥杯省赛A组c++第3题
/* 小明发现了一个奇妙的数字.它的平方和立方正好把0~9的10个数字每个用且只用了一次. 你能猜出这个数字是多少吗? 请填写该数字,不要填写任何多余的内容. */ #include<cstdi ...
- js return false\e.preventDefault() 以及session
@{ ViewBag.Title = "Test"; } <h2>Test</h2> 区别的介绍 <br/> 我们在平时的编码中javascri ...
- SR锁存器
CRM(临界连续模式)BOOST PFC 电路控制系统 SR锁存器 S和R都等于0的时候为什么有两个不同的Q?正因为这样才叫锁存器.Q’是Q的取反,不可能相同.Q*和Q‘不一样.Q是Q*的前一个状态. ...
- LeetCode 811 Subdomain Visit Count 解题报告
题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...
- MAC终端TAB自动补全忽略大小写
打开终端,输入: nano .inputrc 在里面粘贴上以下语句: set completion-ignore-case onset show-all-if-ambiguous onTAB: men ...
- 机器学习:K-Means/K-Means++
- Java 第二次测试总结
Java 第二次测试总结 1. 相关知识点总结 Java测试题循环与递归知识点 补充知识点: for循环语句:for(表达式1:表达式2:表达式3)表达式一负责完成变量的初始化!表达式2是值为bool ...
- POJ1845 sumdiv 数论
正解:小学数学数论 解题报告: 传送门! 其实不难但我数学这个方面太菜了所以还是多写点儿博客趴QAQ 然后因为是英文的所以先翻译一下,,,? 大概就是说求AB的所有约数之和,对9901取膜 这个只需要 ...
- Python 字符串常用方法总结
明确:对字符串的操作方法都不会改变原来字符串的值 1,去掉空格和特殊符号 name.strip() 去掉空格和换行符 name.strip('xx') 去掉某个字符串 name.lstrip() ...