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

     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  * <p>Type: HibernateUtil</p>
    11  * <p>Description: Hibernate工具类.</p>
    12  * @author baitang.<gy03554>
    13  * @date 2018年10月16日 上午12:37:58
    14  * @version v1.0.0
    15  */
    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 SessionFactory
    24      */
    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 SessionFactory
    36      */
    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 SessionFactory
    55      */
    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.xml
    61                 .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 SessionFactory
    67             // 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 SessionFactory
    77      */
    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

  • 相关阅读:
    一次Zookeeper 扩展之殇
    宜信敏捷数据中台建设实践|分享实录
    初学Docker容器网络不得不看的学习笔记
    Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)
    Codeforce-CodeCraft-20 (Div. 2)-A. Grade Allocation
    Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)
    Codeforce-Ozon Tech Challenge 2020-B. Kuroni and Simple Strings(贪心)
    Codeforce-Ozon Tech Challenge 2020-A. Kuroni and the Gifts
    Codeforces Round #509 (Div. 2) A. Heist 贪心
    CodeForces
  • 原文地址:https://www.cnblogs.com/Charles-Yuan/p/9795688.html
Copyright © 2020-2023  润新知