日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Hibernate系列—] 2. 创建SessionFactory 与 Session

發(fā)布時間:2025/6/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Hibernate系列—] 2. 创建SessionFactory 与 Session 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Configuration 對象創(chuàng)建

要創(chuàng)建SessionFactory , 首先要創(chuàng)建Configuration 對象。

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

默認(rèn)狀況下, hibernate會到 classPath 目錄下加載hibernate.cfg.xml 文件。

這里延續(xù)上一篇的例子:

[Hibernate系列—] 1. 下載與試用Hibernate(MySQL與Oracle 配置)

在Eclipse 中進(jìn)行開發(fā)。


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


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

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

內(nèi)容如下:

[html] view plain copy
  • hibernate.dialect=org.hibernate.dialect.MySQLDialect??
  • hibernate.connection.driver_class=com.mysql.jdbc.Driver??
  • hibernate.connection.url=jdbc:mysql://localhost:3306/test??
  • hibernate.connection.username=root??
  • hibernate.connection.password=123456??
  • #hibernate.hbm2ddl.auto=create??

  • 可以看出, 這種方式無法添加 User.hbm.xml 的配置, 所以可以在代碼中添加: [java] view plain copy
  • Configuration?configuration?=?new?Configuration().addResource("com/oscar999/Usr.hbm.xml");??

  • 方式3.? 可以直接在代碼中進(jìn)行設(shè)置, 類似 [java] view plain copy
  • 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");??
  • 也可以通過 [java] view plain copy
  • Configuration?configuration?=?new?Configuration().addClass(com.oscar999.Usr.class)??
  • 添加映射文件。


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


    除此之外, 如果不想使用默認(rèn)的文件名, 也可以這樣:

    [java] view plain copy
  • File?file?=?new?File("src/com/oscar999/myhibernate.xml");????
  • Configuration?config?=?new?Configuration();????
  • config.configure(file);????

  • SessionFactory 對象的創(chuàng)建

    Configuration 創(chuàng)建完成之后, 接下來就是創(chuàng)建 SessionFactory 了。

    在Hibernate 3中,創(chuàng)建SessionFactory 的方式是:

    [java] view plain copy
  • SessionFactory?sessionFactory?=?new?Configuration().configure().buildSessionFactory();??
  • 但是在, Hibernate 4 中, 這種方法已經(jīng)過時了。

    目前推薦的使用方式是:

    [java] view plain copy
  • ServiceRegistry?serviceRegistry?=?new?StandardServiceRegistryBuilder()??
  • ????????.applySettings(configuration.getProperties()).build();??
  • SessionFactory?sessionFactory?=?configuration??
  • ????????.buildSessionFactory(serviceRegistry);??
  • 至于為什么要使用這種方式, 可以參考:

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


    session 的使用

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


    [java] view plain copy
  • 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();??

  • 需要注意的就是, 記得關(guān)閉Session 和 SessionFactory

    總結(jié)

    以上是生活随笔為你收集整理的[Hibernate系列—] 2. 创建SessionFactory 与 Session的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。