Hibernate配置方式
Hibernate給人的感受是靈活的,要達到同一個目的,我們可以使用幾種不同的辦法。就拿Hibernate配置來說,常用的有如下三種方式,任選其一。
- 在 hibernate.cfg.xml 中加入元素 <property>、<mapping>,放置在類路徑(classpath)的根目錄下。
- 將 hibernate.properties 放置放在類路徑的根目錄下。
- 可編程的配置方式,即在程序中配置Hibernate的啟動參數、加載映射文件,需要用Configuration接口來實現這一方式。
使用hibernate.cfg.xml是我比較喜歡的方式,一方面xml天生的優勢——良好的可讀性,讓配置的意圖一目了然。另一方面這是官方推薦使用的,如果同時在hibernate.cfg.xml和hibernate.properties對Hibernate進行了配置,那么前者將覆蓋后者。
hibernate.properties可以非常的簡潔明了,并且有一種linux配置文件的風格。以#開始一行的注釋,用鍵值對的方式存儲配置參數。
對于這兩種方式,結果都是一樣的。只是看個人喜好。關于配置參數我們稍后討論。
Configuration類
org.hibernate.cfg.Configuration實例的作用是對Hibernate進行配置,以及對它進行啟動。在Hibernate的啟動過程中,Configuration類的實例首先讀取Hibernate配置文件,加載配置信息,然后加載映射文件,創建一個SessionFactory對象。
實例被設計成啟動期間(startup-time)對象,一旦SessionFactory 創建完成它就被丟棄了。
要使用一個Configuration對象,要為它設置兩個方面的內容:
- 數據庫連接屬性
- hbm.xml或pojo類
Configuration常用操作函數
1.加載Hibernate配置文件
Configuration cfg=new Configuration().configure("/etc/hibernate.cfg.xml");
或者
Configuration cfg=new Configuration().configure("/etc/hibernate.properties");
2.為Configuration指定映射文件
cfg.addResource("test/User.hbm.xml");
3.為Configuration指定POJO類,Order.hbm.xml根Order.java一個目錄
cfg.addClass(test.Order.class);
4.為Configuration指定Hibernate配置屬性,當然我們加載了配置文件就不能使用這個方法了。
1 2 3 4 5 | Configuration cfg = new Configuration() ???? .addClass(test.User. class ) ???? .setProperty( "hibernate.dialect" , "org.hibernate.dialect.MySQLDialect" ) ???? .setProperty( "hibernate.connection.datasource" , "java:comp/env/jdbc/test" ) ???? .setProperty( "hibernate.order_updates" , "true" ); |
5.獲得SessionFactory
SessionFactory sessions = cfg.buildSessionFactory();
當所有映射定義被 Configuration 解析后,應用程序必須獲得一個用于構造org.hibernate.Session 實例的工廠SessionFactory。這個工廠將被應用程序的所有線程共享,線程安全的全局對象,只需要被實例化一次。單例模式。
Hibernate 允許你的應用程序創建多個SessionFactory 實例。這對 使用多個數據庫的應用來說很有用。
hibernate.cfg.xml
hibernate.cfg.xml在文檔開頭的DTD(文檔類型定義)是很復雜的。我們并不需要去理會和記憶他。你可以直接copy它。
hibernate.cfg.xml文檔以<hibernate-configuration>為根元素,你可以在其子元素<session-factory>中
- 加入<property>元素來配置各種參數。
- 加入<mapping>加載映射文件,resource代表映射文件的路徑。
一個hibernate.cfg.xml例子:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?xml version= '1.0' encoding= 'utf-8' ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > <hibernate-configuration> ???? <session-factory> ???????? <!-- Database connection settings --> ???????? <property name= "connection.url" >jdbc:mysql: //localhost:3306/test</property> ???????? <property name= "connection.username" >root</property> ???????? <property name= "connection.password" >klguang @mysql </property> ???????? <property name= "connection.driver_class" >com.mysql.jdbc.Driver</property> ???????? <!-- SQL dialect --> ???????? <property name= "dialect" >org.hibernate.dialect.MySQLDialect</property> ???????? <!-- JDBC connection pool (use the built-in) --> ???????? <property name= "connection.pool_size" > 10 </property> ???????? <!-- Enable Hibernate's automatic session context management --> ???????? <property name= "current_session_context_class" >thread</property> ???????? <!-- Disable the second-level cache --> ???????? <property name= "cache.provider_class" > ???????????? org.hibernate.cache.NoCacheProvider ???????? </property> ???????? <!-- Echo all executed SQL to stdout --> ???????? <property name= "show_sql" > true </property> ???????? <!-- Drop and re-create the database schema on startup --> ???????? <property name= "hbm2ddl.auto" >update</property> ???????? <property name= "javax.persistence.validation.mode" >none</property> ???????? <mapping resource= "hbm/User.hbm.xml" /> ???????? <mapping resource= "hbm/Event.hbm.xml" /> ???????? <mapping resource= "hbm/Person.hbm.xml" /> ???? </session-factory> </hibernate-configuration> <br> |
hibernate.properties
對于hibernate.properties作為配置文件的方式,我是不推薦新手使用的。因為,其可讀性差,另外眾多的配置參數會讓初學者不知道如何下手。
在Hibernate發布包的project/etc/,提供了一個hibernate.properties文件,該文件列出了Hibernate 的所有配置參數,但都是用#注釋掉了。每一行是一個配置參數,以鍵值對的方式存在,空格前是key,空格后是value,我們應該將空格改為等號。
對每一個配置參數,文件里都有詳細的解釋。我們只需要將見面#去掉,并修改其value就可以了。
一個hibernate.properties例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #數據庫使用的驅動類 hibernate.connection.driver_class=com.mysql.jdbc.Driver #數據庫連接串 hibernate.connection.url=jdbc:mysql: //localhost:3306/db #數據庫連接的用戶名 hibernate.connection.username=user #數據庫連接的密碼 hibernate.connection.password=password #數據庫使用的方言 hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect #是否打印SQL語句 hibernate.show_sql= true javax.persistence.validation.mode=none |
hibernate.properties沒有提供加載映射文件的方式。因此需要通過Configuration的.addResource()方法來加載映射文件或POJO類,Hibernate會自動找到另一方,前提映射文件和POJO類在同一包(目錄)中。
1 2 3 4 | Configuration cfg = new Configuration(); cfg.configure( "/etc/hibernate.properties" ); cfg.addResource( "test/User.hbm.xml" ); cfg.addClass(test.Order. class ); |
Hibernate配置參數詳解
Hibernate JDBC 屬性
屬性名 | 用途 |
hibernate.connection.driver_class | JDBC driver class |
hibernate.connection.url | JDBC URL |
hibernate.connection.username | database user |
hibernate.connection.password | ?數據庫用戶密碼 |
hibernate.connection.pool_size | maximum number of pooled connections |
Hibernate 數據源屬性
屬性名 | 用途 |
hibernate.connection.datasource | 數據源?JNDI 名字 |
hibernate.jndi.url JNDI | 提供者的?URL(可選) |
hibernate.jndi.class JNDI | InitialContextFactory 類(可選) |
hibernate.connection.username | ?數據庫用戶(可選) |
hibernate.connection.password | 數據庫密碼(可選) |
可選的配置屬性
有大量屬性能用來控制 Hibernate 在運行期的行為。它們都是可選的,并擁有適當的默認值。
屬性名 | 用途 | 可選值 ()內為默認 |
hibernate.dialect | 允許?Hibernate?針對特定的關系數據庫生成優化的?SQL?的org.hibernate.dialect.Dialect?的類名。 例如:org.hibernate.dialect.MySQLDialect | ? |
hibernate.show_sql | 輸出所有?SQL?語句到控制臺。 | true|false (false) |
hibernate.format_sql | 在?log?和?console?中打印出更漂亮的?SQL。 | true|false (false) |
hibernate.default_catalog | ?在生成的?SQL?中,將給定的?catalog?附加于非全限定名的表名上 | ? |
hibernate.session_factory_name | org.hibernate.SessionFactory?創建后,將自動使用這個名字綁定到?JNDI?中。 | ? |
hibernate.max_fetch_depth | 為單向關聯(一對一,多對一)的外連接抓取(outer join fetch)樹設置最大深度。 | 0到3 |
hibernate.default_batch_fetch_size | ?為?Hibernate?關聯的批量抓取設置默認數量。 | 4、8、16 |
hibernate.default_entity_mode | ?為由這個?SessionFactory?打開的所有?Session指定默認的實體表現模式。 | dynamic-map,dom4j,pojo |
hibernate.order_updates | ?強制?Hibernate?按照被更新數據的主鍵,為SQL?更新排序。這么做將減少在高并發系統中事務的死鎖。 | true|false ? |
hibernate.generate_statistics | ?如果開啟,Hibernate?將收集有助于性能調節的統計數據。 | true|false ? |
hibernate.use_identifier_rollback | 如果開啟,在對象被刪除時生成的標識屬性將被重設為默認值。 | true|false ? |
hibernate.use_sql_comments | 如果開啟,Hibernate?將在?SQL?中生成有助于調試的注釋信息,默認值為?false。 | true|false (false) |
Hibernate JDBC 和連接(connection)屬性、Hibernate 緩存屬性、Hibernate 事務屬性等主要用于提升性能,并且Hibernate有適當的默認值。入門者可以忽略這些設置,等學到一定階段,你可以參考官方文檔進行適當配置。