正確理解Hibernate Inverse (轉)
通過Hibernate Inverse的設定來決定是由誰來維護表和表之間的關係。最近有朋友問我Hibernate關於多對多關於刪除中間表資料的問題,關鍵是Inverse的設定,下面引用網友的一篇文章。
Inverse是Hibernate雙向關係中的基本概念,當然對於多數實體,我們並不需要雙向關聯,更多的可能會選擇單向關聯,況且我們大多數人 一般採用一對多關係,而一對多雙向關聯的另一端:多對一的Inverse屬性是不存在,其實它預設就是Inverse=false.從而防止了在一對多端 胡亂設定Inverse也不至於出錯。但是Inverse設定不當確實會帶來很大的效能影響,這點是我們必須關注的。
這篇文章已經詳細分析了Hibernate Inverse設定不當帶來的影響:http://www.Hibernate.org/155.html,看了這篇文章,還是很有必要再寫下一些總結的:
1)Hibernate Inverse中提及的side其實是指一個類或者表的概念,雙向關聯其實是指雙方都可以取得對方的應用。
2)維護關係這個名詞還是稍顯模糊或者晦澀。我們一般說A類或者A表(這裡的表的是指多對多的連線表)有責任維護關係,其實這裡的意思是說,我在應 用在更新,建立,刪除(讀就不用說了,雙向引用正是為了方便讀而出現)A類或者A表時,此時建立的SQL語句必須有責任保證關係的正確修改。
3)Inverse=false的side(side其實是指Inverse=false所位於的class元素)端有責任維護關係,而Inverse=true端無須維護這些關係。
4)我們說Hibernate Inverse設立不當會導致效能低下,其實是說Inverse設立不當,會產生多餘重複的SQL語句甚至致使JDBC exception的throw。這是我們在建立實體類關係時必須需要關注的地方。一般來說,Inverse=true是推薦使用,雙向關聯中雙方都設定 Inverse=false的話,必會導致雙方都重複更新同一個關係。但是如果雙方都設立Inverse=true的話,雙方都不維護關係的更新,這也是 不行的,好在一對多中的一端:many-to-one預設是Inverse=false,避免了這種錯誤的產生。但是多對多就沒有這個預設設定了,所以很 多人經常在多對多的兩端都使用Inverse=true,結果導致連線表的資料根本沒有記錄,就是因為他們雙分都沒有責任維護關係。所以說,雙向關聯中最 好的設定是一端為Inverse=true,一端為Inverse=false。一般Inverse=false會放在多的一端,那麼有人提問了, many-to-many兩邊都是多的,Inverse到底放在哪兒?其實Hibernate建立多對多關係也是將他們分離成兩個一對多關係,中間連線一 個連線表。所以通用存在一對多的關係,也可以這樣說:一對多是多對多的基本組成部分。
看下面的多對多的定義大家更會清楚”多對多“與“一對多”的關係:其中我們注意<many-to-many />標籤的特點就知道,它是定義了一個多對多關係,而不是<one-to-many/>。
- <?xml version="1.0"?>
- <!DOCTYPE Hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
- "http://Hibernate.sourceforge.net/Hibernate-mapping-2.0.dtd">
- <Hibernate-mapping package="org.Hibernate.auction">
- <class name="TestA" table="TestA"
- dynamic-update="true" dynamic-insert="true" >
- <id name="id" column="id" type="int" unsaved-value="any" >
- <generator class="assigned">
- </generator>
- </id>
- <property name="name" type="java.lang.String"
- update="true" insert="true" column="name" />
- <set name="testBs" table="TestA_TestB" Inverse="false" cascade="all">
- <key column="testA"/>
- <many-to-many column="testB" class="TestB" />
- </set>
- </class>
- <class name="TestB" table="TestB"
- dynamic-update="true" dynamic-insert="true" >
- <id name="id" column="id" type="int" unsaved-value="any" >
- <generator class="assigned">
- </generator>
- </id>
- <property name="name" type="java.lang.String" update="true"
- insert="true" column="name" />
- <set name="testAs" table="TestA_TestB" Inverse="true" cascade="all">
- <key column="testB"/>
- <many-to-many column="testA" class="TestA" />
- </set>
- </class>
- </Hibernate-mapping>
在對多對中,因為一端維護關係另一端不維護關係的原因,我們必須注意避免在應用中用不維護關係的類建立關係,因為這樣建立的關係是不會在資料庫中儲存的。基於上面的對映檔案程式碼給出一個例子:
- package org.Hibernate.auction;
- import java.util.*;
- /**
- * @author Administrator
- *
- * To change the template for this generated type comment go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- public class TestA {
- int id;
- String name;
- Set testBs=new HashSet();
- public TestA(){
- }
- public TestA(int id){
- setId(id);
- }
- public int getId(){
- return id;
- }
- public void setId(int id){
- this.id=id;
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name=name;
- }
- public Set getTestBs(){
- return testBs;
- }
- public void setTestBs(Set s){
- testBs=s;
- }
- public void addTestB(TestB tb){
- testBs.add(tb);
- }public static void main(String[] args) {
- }
- }
- public class TestB {
- int id;
- String name;
- Set testAs=new HashSet();
- public TestB(){
- }
- public TestB(int id){
- setId(id);
- }
- public int getId(){
- return id;
- }
- public void setId(int id){
- this.id=id;
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name=name;
- }
- public Set getTestAs(){
- return testAs;
- }
- public void setTestAs(Set s){
- testAs=s;
- }
- public void addTestA(TestA ta){
- testAs.add(ta);
- }
- public static void main(String[] args) {
- }
- }
測試程式碼:
- public void doTest() throws Exception{
- TestA a1=new TestA(1);
- TestA a2=new TestA(2);
- TestA a3=new TestA(3);
- TestB b1=new TestB(1);
- TestB b2=new TestB(2);
- TestB b3=new TestB(3);
- a1.addTestB(b1);
- a1.addTestB(b2);
- a1.addTestB(b3);
- b2.addTestA(a1);
- b2.addTestA(a2);
- Session s = factory.openSession();
- s = factory.openSession();
- Session session = factory.openSession();
- session.save(a1);
- session.flush();
- session.close();
- }
測試後連線表的資料為:
testa testb
1 1
1 2
1 3
根據Inverse規則,對這些程式碼:b2.addTestA(a1); b2.addTestA(a2); 建立的關係,資料庫並沒有儲存下來,因為TestB沒有責任維護這些關係,所以產生的sql語句自然不會有針對Testa_testB表的操作了。假設應 用中真的需要這些方法,那麼我們可以修改TestB的方法,讓他們注意在維護端類中執行相應的操作以使得關係能夠在資料庫中儲存下來,更改TestB如 下:
- /*
- * Created on 2004-7-25
- *
- * To change the template for this generated file go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- package org.Hibernate.auction;
- import java.util.*;
- /**
- * @author Administrator
- *
- * To change the template for this generated type comment go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- public class TestB {
- int id;
- String name;
- Set testAs=new HashSet();
- public TestB(){
- }
- public TestB(int id){
- setId(id);
- }
- public int getId(){
- return id;
- }
- public void setId(int id){
- this.id=id;
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name=name;
- }
- public Set getTestAs(){
- return testAs;
- }
- public void setTestAs(Set s){
- testAs=s;
- }
- public void addTestA(TestA ta){
- testAs.add(ta);
- ta.addTestB(this);
- }
- public static void main(String[] args) {
- }
- }
那麼測試執行後連線表的資料為:
testa testb
1 2
1 3
1 1
2 2
測試通過。
相關文章
- Hibernate中的cascade與inverse
- Hibernate【inverse和cascade屬性】知識要點
- 正確理解CAP理論
- 怎樣正確理解volatile?
- 如何正確理解棧和堆?
- 正確理解 PHP 的過載PHP
- background-position的正確理解方式
- 理解並正確使用synchronized和volatilesynchronized
- 正確理解memcached,才能更好的使用
- 談如何正確理解 IP 資料的覆蓋率,兼談正確率~
- 如何正確理解「指標」和「標籤」指標
- 玩轉 Ceph 的正確姿勢
- 如何正確理解Python培訓?有必要嗎?Python
- inverse of arc length
- 轉行轉崗的正確姿勢,你知道嗎?
- 正確理解和使用JAVA中的字串常量池Java字串
- Java 正確的做字串編碼轉換Java字串編碼
- ToString()字串轉換你用正確了嗎?字串
- 編寫高質量的js之正確理解正規表示式回溯JS
- 分散式系統知識分享:正確理解CAP定理分散式
- Mxnet模型轉化為ncnn模型,並驗證轉化正確性模型CNN
- Go 中數字轉換字串的正確姿勢Go字串
- CAD怎麼轉換成PDF格式?CAD轉換PDF的正確方法
- Handler正確用法
- 企業如何正確使用低程式碼轉型升級
- 如何正確理解神經網路在NLP領域的運用神經網路
- 智慧|智慧倉儲就是無人倉庫嗎?正確理解很重要
- 理解玩家的正確姿勢:遊戲到底意味著什麼?遊戲
- Troubleshooting 專題 - 問正確的問題 得到正確的答案
- 使用 pymysql 的時候如何正確的處理轉義字元MySql字元
- 如何正確部署 QUICUI
- 正確高效使用 GoogleGo
- 處理日期和時區轉換:為什麼正確的 UTC 轉換很重要
- 提出問題,解答問題!這才是理解程式碼設計的正確方法
- 如何正確使用async/await?AI
- 如何正確處理nonce
- Idea:一個正確配置Idea
- PHP Opcache 的正確使用PHPopcache
- 如何正確使用 Slim 框架框架