Hibernate最佳實踐(Best Practices)


1、 使用Configuration裝載映射文件時,不要使用絕對路徑裝載。最好的方式是通過getResourceAsStream()裝載映射文件,這樣Hibernate會從classpath中尋找已配置的映射文件。
2、 SessionFactory的創建非常消耗資源,整個應用一般只要一個SessionFactory就夠了,只有多個數據庫的時候纔會使用多個SessionFactory。
3、 在整個應用中,Session和事務應該能夠統一管理。(Spring爲Hibernate提供了非常好的支持)
4、 將所有的集合屬性配置設置爲懶加載(lazy="true")。在hibernate2.x版本中,lazy默認值是"false",但hibernate3.x已經將lazy的默認改爲"true"了。
5、 在定義關聯關係時,集合首選Set,如果集合中的實體存在重複,則選擇List(在定義配置文件時,可以將List定義爲bag),數組的性能最差。
6、 HQL子句本身大小寫無關,但是其中出現的類名和屬性名必須注意大小寫區分。
7、 在一對多的雙向關聯中,一般將集合的inverse屬性設爲true,讓集合的對方維護關聯關係。例如:Group-User,由User來維護Group和User的關聯關係。
8、 在非分佈式架構中,不需要使用DTO來向上層傳輸數據。直接使用POJO的Entity就可以了。
在<many-to-one>中,將outer-join 設置爲false。If the SQL statements use join operations that are too complex and
slow, set outer-join to false for <many-to-one> associations (this is
enabled by default). Also try to tune with the global hibernate.
max_fetch_depth configuration option, but keep in mind that this
is best left at a value between 1 and 4.
9、 如果要精通Hibernate,熟練掌握關係數據庫理論和SQL是前提。
10、 
If the SQL statements use join operations that are too complex and
slow, set outer-join to false for <many-to-one> associations (this is
enabled by default). Also try to tune with the global hibernate.
max_fetch_depth configuration option, but keep in mind that this
is best left at a value between 1 and 4.

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章