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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM