不同版本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 ...
随机推荐
- Deck of Cards ZOJ - 2852 dp 多决策 三维 滚动更新
题意:一个特殊21点游戏 具体http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2852 题解:建一个三维dp,表示三个卡槽分别 ...
- gitignore 不起作用的解决办法
gitignore 不起作用的解决办法 - sloong - 博客园 https://www.cnblogs.com/sloong/p/5523244.html Administrator@PC-20 ...
- [math][mathematica] Mathematica进阶
1. Mathematica 画函数图像 2. Mathematica 解方程 见截图,敲完一行按Shift+Enter就可以执行了.主要函数名都是大小写敏感的.写的正确会跟有提示下拉框和相信说明,非 ...
- visual stodio 编译前后动作定制总结
copy "$(TargetDir)$(TargetName).lib" ..\lib\deploy\$(TargetName).lib 编译完成后将一个.lib 文件拷贝到指定目 ...
- iOS 修改TabBar的item间距
@interface HPTabBarController ()<UITabBarControllerDelegate>//继承自UITabBarController @property ...
- 转:ArcGIS API for JavaScript之图层
参考文章地址: https://developers.arcgis.com/javascript/3/jsapi/layer-amd.html Layer |–TiledMapServiceLayer ...
- Ubuntu 下Anaconda3出现 conda:command not found(未找到命令)
问题:anaconda: command not found解决方案:打开Terminal 1.使用命令:sudo apt install vim 安装vim文本编辑器2.使用命令:vim ~/.ba ...
- Android Studio 下载与安装配置
一.下载 Android Studio下载链接:http://www.android-studio.org/ 1.进入安卓中文社区官网进行下载. 2.下载完成. 二.安装与配置环境 1.选择“此电脑” ...
- MySQL Community Server 8.0.11下载与安装配置
一.下载 1.选择合适的安装包,我在这里下载的是目前最新的安装包,8.0.11,而且我选择下载的是解压版的,安装版的话,安装会比较麻烦. MySQL Community Server下载链接:http ...
- centos安装Django之一:安装openssl
这几天在部署Django,需要安装的东西有点多,python3.pip3.openssl(pip依赖ssl环境),所以第一步是安装openssl,如何安装呢?主要有三步,随ytkah一起来看看吧 1. ...