hibernate中Configuration類的作用


問題:我們在獲得一個SessionFactory對象的時候經常是寫下面這行代碼:

1   SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

那么這行代碼到底有什么作用,Configuration的對象的作用是什么?

要回答上述問題必須首先知道Configuration對象的作用。

Configuration的作用是:An instance of org.hibernate.cfg.Configuration represents an entire set of mapping of an application's java types to an SQL database.

The org.hibernate.cfg.Configuratio is used to build an immutable org.hibernate.SessionFactory.the mappings are compiled from various xml mapping files.

知道Configuration的對象的作用之后,我們完全可以不要配置文件hibernate.cfg.xml以及在里面進行配置。只需要在一個類中進行加載屬性和domain對象的映射文件即可。

首先看這個項目的目錄結構如圖所示:

其中Person是一個domain對象,Person3.hbm.xml是Person對象與表關聯的一個配置文件。Test10是一個測試類,該測試類主要是測試在沒有用hibernate.cfg.xml的文件下對數據進行更新。

Person類的代碼如下:

 1 package com.qls.domain;
 2 
 3 import java.util.Date;
 4 
 5 /**
 6  * Created by 秦林森 on 2017/5/21.
 7  */
 8 public class Person {
 9     private Integer id;
10     private String  name;
11     private Date enterCampusDate;
12 
13     public Integer getId() {
14         return id;
15     }
16 
17     public void setId(Integer id) {
18         this.id = id;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public Date getEnterCampusDate() {
30         return enterCampusDate;
31     }
32 
33     public void setEnterCampusDate(Date enterCampusDate) {
34         this.enterCampusDate = enterCampusDate;
35     }
36 }

Person3.hbm.xml文件的代碼如下:

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.qls.domain">
 7     <class name="Person" table="person">
 8 <id name="id" column="person_id">
 9 <generator class="native"/>
10 </id>
11 <property name="name"/>
12 <property name="enterCampusDate" type="timestamp"/>
13         </class>
14         </hibernate-mapping>

Test10類的代碼如下:

 1 package com.qls.test;
 2 
 3 import com.qls.domain.Person;
 4 import org.hibernate.Session;
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.Transaction;
 7 import org.hibernate.cfg.Configuration;
 8 
 9 /**
10  * Created by ${秦林森} on 2017/5/22.
11  */
12 public class Test10 {
13     public static void main(String[] args) {
14         Configuration configuration = new Configuration();
15         //set database connection.
16         configuration
17                 .addResource("/com/qls/configurationFile/Person3.hbm.xml")//com前面的斜杠不能省略。一定要寫成/com的形式。
18                 .setProperty("hibernate.show_sql", "true")
19                 .setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver")
20                 .setProperty("hibernate.connection.url", "jdbc:oracle:thin:@localhost:1521:orcl")
21                 .setProperty("hibernate.connection.username", "scott")
22                 .setProperty("hibernate.connection.password", "a123456")
23                 //設置方言屬性
24                 .setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
25                 .setProperty("hibernate.connection.pool_size", "10");
26         SessionFactory sessionFactory = configuration.buildSessionFactory();
27         Session session = sessionFactory.openSession();//得到會話。
28         Transaction tx = session.beginTransaction();//開啟事務
29         Person p = session.get(Person.class, 24);
30         //更新數據。
31         p.setName("沉魚");
32         session.update(p);
33         tx.commit();
34     }
35 }

 

至於new Configuration().configure()打開源碼就可以看到了,他是讀取src下的配置文件hibernate.cfg.xml.

 


免責聲明!

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



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