我的Blog我做主^_^

          走向一條通往JAVA的不歸路...

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            64 隨筆 :: 68 文章 :: 77 評論 :: 0 Trackbacks

          一、PO的數(shù)據類型設置
          int 還是Integer Integer 允許為 null
          Hibernate 既可以訪問Field也可以訪問Property,訪問Property是只是調用getXXX()、setXXX()方法,因此在from Customer where c.name=’Tom’ HQL中,name屬性不需要存在,只要getName()存在就可以了。

          二、Hibernate映射

          1、映射復合主鍵

          代碼
          1. 主鍵類 ??
          2. Public?class?CustomerId?implements?Serializable{ ??
          3. ????Private?final?String?name; ??
          4. ????Private?final?String?companyid; ??
          5. } ??
          6. 映射文件 ??
          7. < class ? name =”test.Customer”? table =”CUSTOMERS” > ??
          8. ???? < composite-id ? name =”customerId”? class =”test.CustomerId” > ??
          9. ???????? < key-property ? name =”name”? column =”NAME”? type =”string” /> ??
          10. ???????? < key-property ? name =”companyId”? column =”COMPANY_ID”?? type =”long” /> ??
          11. ???? </ composite-id > ??
          12. ???? < version ? name =”varsion”? column =”VERSION”? unsaved-value =”0” /> ??
          13. ???? < many-to-one ? name =”company”? class =”test.Company”? column =”COMPANY_ID”? insert =”false”? update =”false” /> ??
          14. ???? < set ? name =”orders”? lazy =”true”? inverse =”true” > ??
          15. ???????? < key > ??
          16. ???????????? < column column =”NAME” /> ??
          17. ???????????? < column column =”COMPANY_ID” /> ??
          18. ???????? </ key > ??
          19. ???? </ set > ??
          20. </ class > ??
          21. < class ? name =”test.Order”? table =”O(jiān)RDERS” > ??
          22. ???? < many-to-one ? name =”customer”? class =”test.Customer” > ??
          23. ???????????? < column column =”NAME” /> ??
          24. ???????????? < column column =”COMPANY_ID” /> ??
          25. ???? </ many-to-one > ??
          26. </ class > ??
          27. ??
          28. 或 ??
          29. ??
          30. < class ? name =”test.Customer”? table =”CUSTOMERS” > ??
          31. ???? < composite-id ? name =”customerId”? class =”test.CustomerId” > ??
          32. ???????? < key-property ? name =”name”? column =”NAME”? type =”string” /> ??
          33. < key-many-to-one ? name =”company”? class =”test.Company”? column =”COMPANY_ID” /> ??
          34. ??
          35. ???? </ composite-id > ??
          36. ???? < version ? name =”varsion”? column =”VERSION”? unsaved-value =”0” /> ??
          37. ???? < set ? name =”orders”? lazy =”true”? inverse =”true” > ??
          38. ???????? < key > ??
          39. ???????????? < column column =”NAME” /> ??
          40. ???????????? < column column =”COMPANY_ID” /> ??
          41. ???????? </ key > ??
          42. ???? </ set > ??
          43. </ class > ??
          44. < class ? name =”test.Order”? table =”O(jiān)RDERS” > ??
          45. ???? < many-to-one ? name =”customer”? class =”test.Customer” > ??
          46. ???????????? < column column =”NAME” /> ??
          47. ???????????? < column column =”COMPANY_ID” /> ??
          48. ???? </ many-to-one > ??
          49. </ class > ??

          2、映射組成關系

          代碼
          1. < hibernate-mapping > ??
          2. ???? < class ? name =”Customer”? table =”CUSTOMERS” > ??
          3. < property ? /> ??
          4. ???????? < component ? name =”homeAddress”? class =”Address” > ??
          5. ???????????? < parent ? name =”customer” /> ??
          6. ???????????? < property /> ??
          7. ???????? </ component > ??
          8. ???????? < component ? name =”comAddress”? class =”Address” > ??
          9. ???????????? < parent ? name =”customer” /> ??
          10. ???????????? < property /> ??
          11. ???????? </ component > ??
          12. ???? </ class > ??
          13. </ hibernate-mapping > ??
          14. ??
          15. Public?class?Customer?implements?Serializable{ ??
          16. ????Address?homeAddress; ??
          17. ????Address?comAddress; ??
          18. } ??
          19. Public?class?Address?implements?Serializable{//是VO不是PO不能單獨Save,也不能關聯(lián)。 ??
          20. ????Customer?customer; ??
          21. }??

          3、映射聚合關系

          代碼
          1. < set /idbag? name =”images”? table =”IMAGES”? lazy =”true” > ??
          2. ???? < key ? column =”CUSTOMER_ID” /> ??
          3. ???? < composite-element ? class =”Image” > ??
          4. ???????? < parent ? name =”customer” /> ??
          5. ???????? < property /> ??
          6. < property /> ??
          7. ???? </ composite-element > ??
          8. </ set /idbag > ??
          9. ??
          10. < map ? name =”images”? table =”IMAGES”? lazy =”true” > ??
          11. ???? < key ? column =”CUSTOMER_ID” /> ??
          12. ???? < index ? type =”string”? column =”IMAGE_NAME” /> ??
          13. ???? < composite-element ? class =”Image” > ??
          14. ???????? < parent ? name =”customer” /> ??
          15. ???????? < property /> ??
          16. < property /> ??
          17. ???? </ composite-element > ??
          18. </ map ? > ??

          4、映射繼承關系

          代碼
          1. DOClass{ ??
          2. ???id ??
          3. } ??
          4. ClassA?extends?DOClass{ ??
          5. ????A1 ??
          6. } ??
          7. ??
          8. ClassC?extends?ClassA{ ??
          9. ????C1 ??
          10. } ??
          11. ??
          12. ClassD?extends?ClassA{ ??
          13. ????D1 ??
          14. } ??
          15. ??
          16. ClassG?extends?ClassD{ ??
          17. ????G1 ??
          18. } ??
          19. ??
          20. ClassH?extends?ClassD{ ??
          21. ????H1 ??
          22. } ??
          23. ??
          24. ClassB?extends?DOClass{ ??
          25. ????B1 ??
          26. } ??
          27. ??
          28. ClassE?extends?ClassB{ ??
          29. ????E1,e2,e3,e4,e5,e6 ??
          30. } ??
          31. ??
          32. ClassF?extends?ClassB{ ??
          33. ????F1,f2,f3,f4,f5,f6,f7 ??
          34. } ??
          35. ??
          36. TABLE_A?{ID(PK),A_TYPE(discriminator),A1,C1,D1,G1,H1} ??
          37. TABLE_B?{ID(PK),B1} ??
          38. TABLE_E?{B_ID(PK/FK),E1,E2,E3,E4,E5,E6} ??
          39. TABLE_F?{B_ID(PK/FK),F(xiàn)1,F2,F3,F4,F5,F6,F7} ??
          40. ??
          41. ClassA.hbm.xml ??
          42. < hibernate-mapping > ??
          43. ???? < class ? name =”ClassA”? table =”TABLE_A”? discriminator-value =”A” > ??
          44. ???????? < id /> ??
          45. ???????? < discriminator ? column =”A_TYPE”? type =”string” /> ??
          46. ???????? < property ? name =”a1”? column =”A1” /> ??
          47. ???????? < sub-class ? name =”ClassC”? discriminator-value =”C” > ??
          48. ???????????? < property ? name =”c1”? column =”C1” /> ??
          49. ???????? </ sub-class > ??
          50. < subclass ? name =”ClassD”? discriminator-value =”D” > ??
          51. ???????????? < property ? name =”d1”? column =”D1” /> ??
          52. ???????????? < subclass ? name =”ClassG”? discriminator-value =”G” > ??
          53. ???????????????? < property ? name =”g1”? column =”G1” /> ??
          54. ???????????? </ subclass > ??
          55. ???????????? < subclass ? name =”ClassH”? discriminator-value =”H” > ??
          56. ???????????????? < property ? name =”h1”? column =”H1” /> ??
          57. ???????????? </ subclasss > ??
          58. </ subclass > ??
          59. </ class > ??
          60. </ hibernate-mapping > ??
          61. ClassB.hbm.xml ??
          62. < hibernate-mapping > ??
          63. ???? < class ? name =”ClassB”? table =”TABLE_B” > ??
          64. ???????? < id /> ??
          65. ???????? < property ? name =”b1”? column =”B1” /> ??
          66. ???????? < joined-subclass ? name =”ClassE”? table =”TABLE_E” > ??
          67. ???????????? < key ? column =”B_ID” /> ??
          68. ???????????? < property ? name =”e1”? column =”E1” /> ??
          69. ???????????? < property ? name =”e2”? column =”E2” /> ??
          70. ???????????? < property ? name =”e3”? column =”E3” /> ??
          71. ???????????? < property ? name =”e4”? column =”E4” /> ??
          72. ???????????? < property ? name =”e5”? column =”E5” /> ??
          73. ???????????? < property ? name =”e6”? column =”E6” /> ??
          74. ???????? </ joined-subclass > ??
          75. ???????? < joined-subclass ? name =”ClassF”? table =”TABLE_F” > ??
          76. ???????????? < key ? column =”B_ID” /> ??
          77. ???????????? < property ? name =”f1”? column =”F1” /> ??
          78. ???????????? < property ? name =”f2”? column =”F2” /> ??
          79. ???????????? < property ? name =”f3”? column =”F3” /> ??
          80. ???????????? < property ? name =”f4”? column =”F4” /> ??
          81. ???????????? < property ? name =”f5”? column =”F5” /> ??
          82. ???????????? < property ? name =”f6”? column =”F6” /> ??
          83. ???????????? < property ? name =”f7”? column =”F7” /> ??
          84. ???????? </ joined-subclass > ??
          85. ???? </ class > ??
          86. </ hibernate-mapping > ??

          5、映射Bag,List和Map

          IDBag

          代碼
          1. IMAGES{ID(PK),CUSTOMER_ID(FK),F(xiàn)ILENAME} ??
          2. List? images ?=? new ?ArrayList(); ??
          3. Customer.hbm.xml ??
          4. ??
          5. < idbag ? name =”images”? table =”IMAGES”? lazy =”true” > ??
          6. ???? < collection-id ? type =”long”? column =”ID” > ??
          7. ???????? < generator ? class =”increment” /> ??
          8. ???? </ collection-id > ??
          9. ???? < key ? column =”CUSTOMER_ID” /> ??
          10. ???? < element ? column =”FILENAME”? type =”string”? not-null =”true” /> ??
          11. </ idbag > ??

          List

          代碼
          1. IMAGES{CUSTOMER_ID(PK/FK),POSITION(PK),F(xiàn)ILENAME} ??
          2. List? images ?=? new ?ArrayList(); ??
          3. Customer.hbm.xml ??
          4. < list ? name =”images”? table =”IMAGES”? lazy =”true” > ??
          5. ???? < index ? column =”POSITION” /> ??
          6. ???? < key ? column =”CUSTOMER_ID” /> ??
          7. ???? < element ? column =”FILENAME”? type =”string”? not-null =”true” /> ??
          8. </ list > ??

          Map

          代碼
          1. IMAGES{CUSTOMER_ID(PK/FK),IMAGE_NAME(PK),F(xiàn)ILENAME} ??
          2. Map? images ?=? new ?HashMap(); ??
          3. < map ? name =”images”? table =”IMAGES”? lazy =”true” > ??
          4. ???? < key ? column =”CUSTOMER_ID” /> ??
          5. < index ? column =”IMAGE_NAME”? type =”string” /> ??
          6. ???? < element ? column =”FILENAME”? type =”string”? not-null =”true” /> ??
          7. </ map > ??
          8. ??
          9. Set?idbag?map?支持數(shù)據庫排序??order? by ?=”ID” ??
          10. Set?map?支持內存排序?? sort ?=?“MyComparator”??

          6、映射一對一關聯(lián)關系特殊情況一

          代碼
          1. Public? class ?Customer{ ??
          2. ????Address?homeAddress; ??
          3. ????Address?comAddress; ??
          4. } ??
          5. ??
          6. Customer.hbm.xml ??
          7. <many-to-one?name=”homeAddress”? class =”Address”?column=”HOME_ADDRESS_ID”?cascade=”all”?unique=” true ”/> ??
          8. <many-to-one?name=”comAddress”? class =”Address”?column=”COM_ADDRESS_ID”?cascade=”all”?unique=” true ”/> ??
          9. ??
          10. Address.hbm.xml ??
          11. <one-to-one?name=”address”? class =”Customer”?property-ref=”homeAddress”/>??

          映射一對一關聯(lián)關系主鍵映射

          代碼
          1. Customer.hbm.xml ??
          2. < one-to-one ? name =”address”? class =”Address”? cascade =”all” /> ??
          3. Address.hbm.xml ??
          4. < class ? name =”address” > ??
          5. ???? < id > ??
          6. ???????? < generator ? class =”foreign” > ??
          7. ???????????? < param ? name =”property” > customer </ param > ??
          8. ???????? </ generator > ??
          9. ???? </ id > ??
          10. < one-to-one ? name =”customer”? class =”Customer”? constrained =”true” /> ??
          11. </ class > ??

          7、映射一對多關聯(lián)

          代碼
          1. < class ? name = "Person" > ??
          2. < id ? name = "id" ? column = "personId" > ??
          3. ???????? < generator ? class = "native" /> ??
          4. </ id > ??
          5. < many-to-one ? name = "address" ? column = "addressId" ? not-null = "true" /> ??
          6. </ class > ??
          7. ??
          8. < class ? name = "Address" > ??
          9. < id ? name = "id" ? column = "addressId" > ??
          10. < generator ? class = "native" /> ??
          11. </ id > ??
          12. < set ? name = "people" ? inverse = "true" > ??
          13. ?????? < key ? column = "addressId" /> ??
          14. < one-to-many ? class = "Person" /> ??
          15. </ set > ??
          16. </ class > ??

          8、映射多對多關聯(lián)

          代碼
          1. < set ? name =”items”? table =”CATEGORY_ITEM”? lazy =”true”? cascade =”save-update” > ??
          2. ???? < key ? column =”CATEGORY_ID” > ??
          3. ???? < many-to-many ? class =”Item”? column =”ITEM_ID” /> ??
          4. </ set > ??

          三、Inverse與cascade
          Inverse
          應該將Set的inverse屬性設置為true,如果為many-to-many 需要將一方設置為true
          如Customer:Order為1:N雙向關聯(lián),將Customer的Set的inverse設置為true,表示Customer與Order之間的關聯(lián)關系由Order端來維護,如customer.getOrders().addOrder(o)不會更新Customer與Order之間的關聯(lián)關系,而order.setCustomer(o)才會更新Customer與Order之間的關聯(lián)關系。

          Cascade
          Save-update 保存、更新Customer會同步更新Order.
          Delete 同步刪除
          All 包含save-update和delete操作,另外調用當前對象的evice或者lock時,對關聯(lián)對象也調用相應方法。
          Delete-orphan 刪除所有和當前對象解除關聯(lián)關系的對象。
          All-delete-orphan 當關聯(lián)雙方為父子關系是(父親控制孩子的持久化生命周期),如果父方刪除,子方自動刪除(同delete),如果子方無父親,子方應刪除。包含Delete和all-orphan的行為。

          四、Hibernate緩存

          Session 緩存(一級緩存),每一session確保自己的緩存的所有的持久對象唯一
          通過調用session.setFlushMode()可設定緩存的清理模式,緩存的清理模式有三種:
          FlushMode.AUTO:query、commit和flush的時候清理緩存。
          FlushMode.COMMIT:commit和flush的時候清理緩存。
          FlushMode.NEVER:只有在調用session.flush()的時候才清理緩存。
          Session 只有在清理緩存的時候才會執(zhí)行相應的sql操作。
          可以使用session.evict()和session.clear()清空緩存。
          Save、update、query都加入Session緩存
          Select c.ID,c.Name,c.age,o.ORDER_NUM,o.CUSTOMER_ID from Customer c,inner join c.orders c 除外。

          SessionFactory緩存(二級緩存)

          代碼
          1. < class ? name =”Category”? table =”CATEGORYS” > ??
          2. ???? < cache ? usage =”read-write” /> ??
          3. ???? < id /> ??
          4. ???? < set ? name =”items”? inverse =”true”? lazy =”true” > ??
          5. ???????? < cache ? usage =”read-write” /> ??
          6. ???????? < key /> ??
          7. ???? </ set > ??
          8. </ class > ??
          9. < class ? name =”Item” > ??
          10. ???? < cache ? usage =”read-write” /> ??
          11. ???? < id /> ??
          12. </ class > ??
          13. ??
          14. Hibernate.cache.provider =…………EhCacheProvider ??
          15. Hibernate.cache.user_query_cache = true ??
          16. ??
          17. Ehcache.xml ??
          18. < ehcache > ??
          19. ???? < diskStore ? path =”c:\\temp” /> ??
          20. ???? < defaultCache ??
          21. ???????? maxElementsInMemory =”10000” ??
          22. ???????? eternal =”false” ??
          23. ???????? timeToIdleSeconds =”120” ??
          24. ???????? timeToLiveSeconds =”120” ??
          25. ???????? overflowToDisk =”true” /> ??
          26. ???? < cache ? name =”Category” ??
          27. ???????? maxElementsInMemory =”10000” ??
          28. ???????? eternal =”false” ??
          29. ???????? timeToIdleSeconds =”120” ??
          30. ???????? timeToLiveSeconds =”120” ??
          31. ???????? overflowToDisk =”true” /> ??
          32. ??
          33. ???? < cache ? name =”Category.Items” ??
          34. ???????? maxElementsInMemory =”10000” ??
          35. ???????? eternal =”false” ??
          36. ???????? timeToIdleSeconds =”120” ??
          37. ???????? timeToLiveSeconds =”120” ??
          38. ???????? overflowToDisk =”true” /> ??
          39. ??
          40. ???? < cache ? name =”Item” ??
          41. ???????? maxElementsInMemory =”10000” ??
          42. ???????? eternal =”false” ??
          43. ???????? timeToIdleSeconds =”120” ??
          44. ???????? timeToLiveSeconds =”120” ??
          45. ???????? overflowToDisk =”true” /> ??
          46. ??
          47. ???? < cache ? name =”customerQueries”…. /> ?<!—設置查詢緩存? ??
          48. ??
          49. </ ehcache > ??

          Query q = session.createQuery();
          q.setCacheable(true);
          q.setCacheRegion(“customerQueries”);

          SessionFactory.evict(),SessionFactory.evictCollection()清除二級緩存。

          直接調用JDBC API不會使用任何緩存。
          二級緩存適合查詢較多但是很少更新的情況。

          盡量對數(shù)據庫的所有操作由Hibernate來完成,而不要用其它方式對數(shù)據庫進行操作,否則可能與緩存沖突,當然如果對緩存有深入研究的除外。

          五、臨時對象(Transient Object)、持久對象(Persistent Object)和游離對象(Detached Object)
          臨時對象:表示對象的主鍵不存在(OID不存在),Hibernate通過key的unsave-value或者version的unsaved-value來判斷是否為臨時對象。Session對臨時對象的唯一操作應該是save()。
          持久對象:在session緩存中存在持久對象,數(shù)據庫中存在相應紀錄。
          游離對象:數(shù)據庫中有相應紀錄,session中不存在持久對象,可通過session.evict()獲得。
          Session緩存中存在,數(shù)據庫中不存在,這是什么類型的對象?實際這種情況不存在,因為所有的Session操作均在事務中進行,緩存中的數(shù)據是通過save、update或者query生成,而save或者update得到的是數(shù)據庫的獨占鎖,因此其它事務沒有可能刪除數(shù)據庫中的數(shù)據。而query獲得的是數(shù)據庫的共享鎖,因此其它事務也不可能獲得獨占鎖來更新數(shù)據。因此在一個事務內部session緩存才有意義,如果脫離事務,僅僅是只讀操作也可能導致session緩存中存在數(shù)據庫中根本不存在相應紀錄的持久性對象。

          六、Hibernate 的檢索策略

          設定批量檢索數(shù)量 batch-size
          外連接深度控制hibernate.max_fetch_depth

          類級別檢索 load、get和find。其中l(wèi)oad可以設置延遲檢索(cglib生成代理類,可通過Hibernate.initialize()初始化),這也是load和get的區(qū)別之一。Get/find立即檢索,與是否設置延遲無關。
          關聯(lián)檢索 立即檢索,延遲檢索,迫切左外連接檢索。Set/list/map等,無論是否延遲檢索得到的都是代理集合類。而非HashSet,ArrayList等。

          Lazy與outer-joint
          False,false 立即檢索
          False,true 迫切左外連接,
          True,false 延遲檢索

          Many-to-one 的outer-join屬性
          Auto:Customer的lazy為true則延遲加載,否則迫切左外連接
          True:迫切左外連接
          False:延遲加載或者立即加載由Customer的lazy決定。
          One-to-one的延遲加載策略
          <one-to-one name=”customer” class=”Customer” constrained=”true”/>
          HQL會忽略迫切左外連接檢索和lazy(只有l(wèi)oad才為代理對象)策略。
          Session.find(“from Customer c as c left join fetch c.orders where c.id=1”)

          Hibernate的檢索方式
          HQL、NativeSql和QBC
          From Customer c inner join c.orders o 查詢結果保存到session緩存
          Select c.ID,c.Name,c.age,o.ORDER_NUM,o.CUSTOMER_ID from Customer c,inner join c.orders c查詢結果不存入Session緩存。

          七、Hibernate并發(fā)控制
          樂觀鎖:VERSION或者timestamp控制,session.lock()立刻進行版本檢查,session.update(),update的時候執(zhí)行版本檢查。
          悲觀鎖:select for upload,session.get(Customer.class,new Long(1),LockMode.UPGRADE)

          總結:本文絕大多數(shù)為摘錄內容,有些地方加上自己的理解,有不對之處懇請批評指正。看了書,閱讀了相關帖子后,感覺學習Hibernate的重點應該是Hibernate的緩存策、查詢和如何提高性能方面。

          另外說點自己的感受,本人做項目到現(xiàn)在都是在設計階段先有關系模型后有對象模型(其實一個Table一個對象),在這種情況下Hibernate的優(yōu)勢大大降低了,其實是為了Hibernate而Hibernate了,個人感覺在先有關系模型的情況下用Hibernate的意義不大。

          如果按照OOAD的開發(fā)流程先有對象模型,然后根據對象模型生成關系模型,那應該說用Hibernate用對了地方。畢竟Hibernate對繼承、多態(tài),各種復雜的關系都有很好的支持。

          http://www.javaeye.com/topic/42854



          posted on 2007-01-22 15:11 java_蟈蟈 閱讀(613) 評論(0)  編輯  收藏 所屬分類: HIBERNATE
          主站蜘蛛池模板: 马山县| 峨眉山市| 嘉善县| 青川县| 甘肃省| 宁陵县| 丰原市| 临夏市| 宕昌县| 都昌县| 平原县| 红安县| 枣庄市| 壶关县| 庆城县| 巩义市| 黄大仙区| 望城县| 丰台区| 错那县| 济南市| 秦皇岛市| 弥渡县| 唐山市| 翁源县| 清原| 江源县| 黄梅县| 新河县| 齐河县| 察雅县| 呼伦贝尔市| 黑山县| 阿拉善盟| 松江区| 长兴县| 晋城| 剑阁县| 资源县| 淮阳县| 海丰县|