小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Hibernate annotation配置方式的MappedBy使用詳解

 wayne_liberary 2014-08-06

Hibernate annotation配置方式的MappedBy使用詳解

說是使用詳解,其實是我自己做了一些簡單的試驗.如有說錯,請指正.

Annotation方式的MappedBy其實就是xml方式的inverse

http:///questions/10082434/mappedby-and-inverse-attributes-are-same 寫道
In case of one-one or one-many or many to one, the key will be stored in one of the entites. It does not make any sense to store it in 2 places.
It essential tells hibernate that the key is at the other end of the relationship.
So if there are entities A and B which have a one to one relationship, and you see the mappedBy attribute in A, then the foreign key (which points to A) is stored in B.

但是對于他們的行為 我一直不是很了解 hibernate里的doc解釋也不是很清楚:

寫道
inverse 映射屬性究竟表示什么呢?對于你和 Java 來說,一個雙向關聯(lián)僅僅是在兩端簡單地正確設置引用。然而,Hibernate 并沒有足夠的信息去正確地執(zhí)行 INSERT 和 UPDATE 語句(以避免違反數(shù)據(jù)庫約束),所以它需要一些幫助來正確的處理雙向關聯(lián)。把關聯(lián)的一端設置為 inverse 將告訴 Hibernate 忽略關聯(lián)的這一端,把這端看成是另外一端的一個鏡象(mirror)

其實簡單點說 就是MappedBy確定"維護關系"的一方,也僅僅是"維護關系"而已.其他的操作不影響.

實驗環(huán)境:hibernate 3.6.10.Final

實體類:User(多方) Classroom(一方) Many-to-One的測試

 

Java代碼: 
  1. //多方:  
  2.     @ManyToOne(cascade=CascadeType.ALL)  
  3.     @JoinColumn(name="cid")  
  4.     public Classroom getClassroom() {  
  5.          return classroom;  
  6.     }  
  7.     public void setClassroom(Classroom classroom) {  
  8.          this.classroom = classroom;  
  9.     }  
Java代碼 :
  1. //一方  
  2.     @OneToMany(mappedBy="classroom")  
  3.     public Set<User> getUsers() {  
  4.         return users;  
  5.     }  
  6.     public void setUsers(Set<User> users) {  
  7.         this.users = users;  
  8.     }   



 

從多方插入就不再累述了.

這里就試著從不維護關系的一方來插入:

Java代碼:  
  1. User user=new User();  
  2. user.setUname("cc");  
  3. user.setUpass("pass");  
  4. Classroom cr=new Classroom();  
  5. cr.setClassname("class one");  
  6.   
  7. Set<User> users=new HashSet<User>();  
  8. users.add(user);  
  9. cr.setUsers(users);  

 這樣只有一條插入語句:

Hibernate: insert into Classroom (cname) values (?)

為了更好地說明問題,我把以上一方的配置進行修改

加入了 cascade=CascadeType.ALL

同樣執(zhí)行以上代碼 顯示插入了兩條:

Hibernate: insert into Classroom (cname) values (?)

Hibernate: insert into User (cid, uname, upass) values (?, ?, ?)

但是查看數(shù)據(jù)庫就會發(fā)現(xiàn):



 

雖然插入了..但是外鍵卻沒有值

一方不維護關系就可以明顯的看出來,

但是一方的其他不影響關系的更新操作是沒有問題的:

Java代碼:  
  1. Classroom cr=(Classroom)session.get(Classroom.class1);  
  2. System.out.println(cr.getUsers().iterator().next().getUname());  
  3. cr.getUsers().iterator().next().setUname("XX");  

 執(zhí)行以上的代碼:

顯示:

Hibernate: select classroom0_.cid as cid1_0_, classroom0_.cname as cname1_0_ from Classroom classroom0_ where classroom0_.cid=?

Hibernate: select users0_.cid as cid1_1_, users0_.uid as uid1_, users0_.uid as uid0_0_, users0_.cid as cid0_0_, users0_.uname as uname0_0_, users0_.upass as upass0_0_ from User users0_ where users0_.cid=?

cc

Hibernate: update User set cid=?, uname=?, upass=? where uid=?

數(shù)據(jù)在數(shù)據(jù)庫里也被正常更新

這樣一番實驗坐下來就可以很清楚的感覺到

mappedBy只是確定了由誰來維護關系而已 其他的操作并不影響.

 

一般情況下 如果要讓兩方都可以維護關系 我自己是在OneToMany后寫JoinColumn 然后和ManyToOne里的外鍵列名取一樣的。

當然也可以不寫JoinColumn 來生成一個第三方表記錄關系 但這樣做沒什么必要 相同的關系會被存兩次.
原文鏈接:http://www./wzjs/java/3296.html

posted on 2013-03-11 09:17 你爸是李剛 閱讀(2770) 評論(0)  編輯  收藏

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多