不同版本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 }
如有问题,欢迎纠正!!!
如有转载,请标明源处: