博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
不同版本Hibernate.获取SessionFactory的方式
阅读量:4665 次
发布时间:2019-06-09

本文共 3582 字,大约阅读时间需要 11 分钟。

 

不同版本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 

1 package com.charles.hibernate.util; 2  3 import org.hibernate.SessionFactory; 4 import org.hibernate.boot.MetadataSources; 5 import org.hibernate.boot.registry.StandardServiceRegistry; 6 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 7 import org.hibernate.cfg.Configuration; 8  9 /**10  * 

Type: HibernateUtil

11 *

Description: Hibernate工具类.

12 * @author baitang.
13 * @date 2018年10月16日 上午12:37:5814 * @version v1.0.015 */16 public class HibernateUtil {17 18 // 配置文件的位置19 private static final String CONFIGURE_XML = "hibernate.cfg.xml";20 21 /**22 * Hibernate 3.x 获取SessionFactory方式.23 * @return SessionFactory24 */25 private static SessionFactory buildHibernate3SessionFactory() {26 27 // 1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息28 Configuration configuration = new Configuration().configure(CONFIGURE_XML);29 30 return configuration.buildSessionFactory();31 }32 33 /**34 * Hibernate 4.x 获取SessionFactory方式35 * @return SessionFactory36 */37 private static SessionFactory buildHibernate4SessionFactory() {38 39 // 1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息40 Configuration configuration = new Configuration().configure(CONFIGURE_XML);41 42 // 2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象43 // hibernate 的任何配置和服务都需要在该对象中注册后才能有效.44 // ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())45 // .buildServiceRegistry();46 // return configuration.buildSessionFactory(serviceRegistry);47 48 return configuration.buildSessionFactory();49 50 }51 52 /**53 * Hibernate 5.x 获取SessionFactory方式54 * @return SessionFactory55 */56 private static SessionFactory buildHibernate5SessionFactory() {57 58 // A SessionFactory is set up once for an application!59 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()60 .configure(CONFIGURE_XML) // configures settings from hibernate.cfg.xml61 .build();62 try {63 return new MetadataSources( registry ).buildMetadata().buildSessionFactory();64 }65 catch (Exception e) {66 // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory67 // so destroy it manually.68 StandardServiceRegistryBuilder.destroy( registry );69 }70 return null;71 }72 73 /**74 * 获取SessionFactory方法75 * @param sessionVersion Hibernate的版本号(取值为:3,4,5)76 * @return SessionFactory77 */78 public synchronized static SessionFactory getSessionFactory(int sessionVersion) {79 80 if (3 == sessionVersion) {81 82 return buildHibernate3SessionFactory();83 } else if (4 == sessionVersion) {84 85 return buildHibernate4SessionFactory();86 } else if (5 == sessionVersion) {87 88 return buildHibernate5SessionFactory();89 }90 return null;91 }92 93 }

 

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

 如有转载,请标明源处:

 

转载于:https://www.cnblogs.com/Charles-Yuan/p/9795688.html

你可能感兴趣的文章
tomcat配置
查看>>
C语言学习之路
查看>>
值-结果参数
查看>>
[Excel] C# ExcelHelper操作类 (转载)
查看>>
使用jsoup进行网页内容抓取
查看>>
深入理解JVM内幕:从基本结构到Java 7新特性
查看>>
[NodeJs]入门经典
查看>>
准确判断listview上下滚动
查看>>
codeforces666A
查看>>
比较真实的下雪效果
查看>>
MongoDB 3.2 从安装到使用。
查看>>
CFround#380 div2
查看>>
设计模式基础知识备忘
查看>>
中国国家气象局天气预报信息接口
查看>>
牛客寒假算法基础集训营2 处女座的砝码 (思维)
查看>>
Samba 3.5.10 发布
查看>>
ORACLE升级PSU&OJVM注意的问题及遇到问题解决思路
查看>>
框架篇:Spring+SpringMVC+hibernate整合开发
查看>>
Masonry教程--IOS自适配,丢掉Autolayout吧
查看>>
java调用.net的webservice接口
查看>>