[Hibernate系列—] 2. 建立SessionFactory 與 Session

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

Configuration 對象建立

要建立SessionFactory , 首先要建立Configuration 對象。

這個對象就是去讀取hibernate 的一些配置資訊。

預設狀況下, hibernate會到 classPath 目錄下載入hibernate.cfg.xml 檔案。

這裡延續上一篇的例子:

[Hibernate系列—] 1. 下載與試用Hibernate(MySQL與Oracle 配置) 在Eclipse 中進行開發。


這個設定檔的方式可以有多種, 可以是xml , 可以是properties , 也可以直接在代碼中寫配置。


方式1.  在src 目錄下放入  hibernate.cfg.xml, 類似上篇的例子

方式2.  在 src 目錄下放入 hibernate.properties

內容如下:

hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.connection.driver_class=com.mysql.jdbc.Driverhibernate.connection.url=jdbc:mysql://localhost:3306/testhibernate.connection.username=roothibernate.connection.password=123456#hibernate.hbm2ddl.auto=create

可以看出, 這種方式無法添加 User.hbm.xml 的配置, 所以可以在代碼中添加:

Configuration configuration = new Configuration().addResource("com/oscar999/Usr.hbm.xml");

方式3.  可以直接在代碼中進行設定, 類似

Configuration configuration = new Configuration().addResource("com/oscar999/Usr.hbm.xml").setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver").setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/test").setProperty("hibernate.connection.username", "root").setProperty("hibernate.connection.password", "123456").setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect").setProperty("hibernate.hbm2ddl.auto", "update");
也可以通過

Configuration configuration = new Configuration().addClass(com.oscar999.Usr.class)
添加對應檔。


一般狀況下, 添加 hibernate.cfg.xml 會比較常用, .properties 和 .xml 也可以並存。


除此之外, 如果不想使用預設的檔案名稱, 也可以這樣:

File file = new File("src/com/oscar999/myhibernate.xml");  Configuration config = new Configuration();  config.configure(file);  


SessionFactory 對象的建立

Configuration 建立完成之後, 接下來就是建立 SessionFactory 了。

在Hibernate 3中,建立SessionFactory 的方式是:

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

但是在, Hibernate 4 中, 這種方法已經過時了。

目前推薦的使用方式是:

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
至於為什麼要使用這種方式, 可以參考:

http://planet.jboss.org/post/hibernate_orm_service_registry


session 的使用

sessionFactory 有了, 接下來就簡單了,直接貼一個例子


Configuration configuration = new Configuration().addClass(com.oscar999.Usr.class);ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);Session session = sessionFactory.openSession();session.beginTransaction();session.save(new Usr("uesr3"));session.getTransaction().commit();session.close();sessionFactory.close();

需要注意的就是, 記得關閉Session 和 SessionFactory


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.