hibernate中Configuration類的作用

來源:互聯網
上載者:User

標籤:項目   pre   present   session   http   pil   src   color   static   

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

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

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

要回答上述問題必須首Crowdsourced Security Testing道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         configuration17                 .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.

 

hibernate中Configuration類的作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.