多對多關聯映射(雙向)

關聯映射方面的最后一篇了,我覺得映射文件的編寫是使用hibernate的基礎,而關聯映射又是基礎的基礎,所以這方面分的細一些,羅嗦一些,說明白就好,呵呵。
多對多關聯(雙向),相對單向,在實體上就是一端也保持另一端的對象集合,在映射文件中也配置上。但是要注意兩邊配置文件中指定的外鍵一定要對應。
1.對象模型:
2.關系模型:
3.實體類:
Player.java
public?class?Player {?
??private?Integer id;?
??private?String name;?
??private?Set<Role> roles;?
??//一系列的setter.getter方法?
??@Override?
??public?String toString() {?
????return?"Player: "?+ name;?
??}?
}
? Role.java
public?class?Role {?
??private?Integer id;?
??private?String name;?
??private?Set<Player> players;?
??//一系列的setter.getter方法?
??@Override?
??public?String toString() {?
????return?"Role: "?+ name;?
??}?
}
4.映射文件:
?Player.hbm.xml
<class?name="com.sxt.hibernate.many2many.entity.Player"?table="sxt_hibernate_player">?
????<id?name="id"?length="4">?
??????<generator?class="native"></generator>?
????</id>?
????<property?name="name"?length="10"></property>?
????<!--table="sxt_hibernate_user_role"含義,用來指定中間表????-->?
????<set?name="roles"?table="sxt_hibernate_player_role"?cascade="save-update">?
??????<!--<key column="user_id">含義,指定中間表中用來指向本表的外鍵????-->?
??????<key?column="player_id"></key>?
??????<!--?column含義,用來指定中間表中用來指向另一端表的外鍵?-->?
??????<many-to-many?class="com.sxt.hibernate.many2many.entity.Role"?column="role_id"></many-to-many>?
????</set>?
??</class>
? Role.hbm.xml
<class?name="com.sxt.hibernate.many2many.entity.Role"?table="sxt_hibernate_role">?
????<id?name="id"?length="4">?
??????<generator?class="native"></generator>?
????</id>?
????<property?name="name"?length="10"></property>?
????<set?name="players"?table="sxt_hibernate_player_role"?cascade="save-update">?
??????<!--?注意這里指定的外鍵要和另一端映射文件中的相對應?-->?
??????<key?column="role_id"></key>?
??????<!--?注意這里指定的外鍵要和另一端映射文件中的相對應?-->?
??????<many-to-many?class="com.sxt.hibernate.many2many.entity.Player"?column="player_id"></many-to-many>?
????</set>?
??</class>
5.hibernate配置文件:
參照上一篇。
6. 測試方法:
public?static?void?main(String[] args) {?
????Session session = HibernateUtils.getSession();?
????Transaction t = session.beginTransaction();?
????try?{?
??????/**?
????????* 測試插入數據?
????????*/
????????
/*??????Player player1=new Player();?
??????player1.setName("姚明");?
????????
??????Player player2=new Player();?
??????player2.setName("詹姆斯");?
????????
??????Player player3=new Player();?
??????player3.setName("科比");?
????????
??????Role role1=new Role();?
??????role1.setName("中鋒");?
??????Set<Player> players1=new HashSet<Player>();?
??????players1.add(player1);?
??????players1.add(player2);?
??????role1.setPlayers(players1);?
????????
??????Role role2=new Role();?
??????role2.setName("后衛");?
??????Set<Player> players2=new HashSet<Player>();?
??????players2.add(player2);?
??????players2.add(player3);?
??????role2.setPlayers(players2);?
????????
??????session.save(role1);?
??????session.save(role2);*/
?
????????
??????/**?
????????* 測試加載數據?
????????*/
?
??????Role role=(Role)session.load(Role.class, 4);?
??????System.out.println(role);?
??????for(Iterator<Player> players=role.getPlayers().iterator();players.hasNext();){?
????????System.out.println(players.next());?
??????}?
??????t.commit();?
????}?catch?(HibernateException e) {?
??????e.printStackTrace();?
??????t.rollback();?
????}?finally?{?
??????HibernateUtils.closeSession(session);?
????}?
??}?
}?



? ? ?本文轉自NightWolves 51CTO博客,原文鏈接:http://blog.51cto.com/yangfei520/276187,如需轉載請自行聯系原作者



本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/256622.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256622.shtml
英文地址,請注明出處:http://en.pswp.cn/news/256622.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

sort-排座椅

題目描述 Description上課的時候總有一些同學和前后左右的人交頭接耳&#xff0c;這是令小學班主任十分頭疼的一件事情。不過&#xff0c;班主任小雪發現了一些有趣的現象&#xff0c;當同學們的座次確定下來之后&#xff0c;只有有限的D對同學上課時會交頭接耳。同學們在教室中…

JSONModel的基本使用

JSONModel 是一個庫,它能智能并且快速的創建出數據 model,你可以在你的 iOS 項目或者 OSX 項目上使用它。 使用前準備 添加 JSONModel 到你的工程中 1、需要的環境: ARC,iOS 5.0 / OSX 10.7 引入框架SystemConfiguration.framework2、獲取途徑&#xff1a; 1&#xff09;、通過…

圖像處理 伽瑪校正

http://blog.csdn.net/lichengyu/article/details/20840135 本質上是關于灰度的一個冪函數&#xff0c;當系數gamma大于1時&#xff0c;低灰度值的動態范圍減小&#xff0c;高灰度值的動態范圍增大&#xff0c;整體的灰度值減小&#xff1b;gamma小于1時則相反&#xff1b; 人…

matlab常用工具箱的調用指令

轉自:http://blog.sina.com.cn/s/blog_86186c970102va9g.html Matlab常用工具箱的調用命令 1. 優化工具箱?? 用途:優化問題 調用命令:在Command Window輸入“optimtool”?,其窗口如下 圖1 Optimization Tool? 2. 神經網絡工具箱? 用途:數據擬合、模式識別和分類…

tomcat起不來的問題已經解決

tomcat 起不來的問題:\apache-tomcat-6.0.10\bin 中startup.bat 起不來&#xff0c;一閃就沒了。說明&#xff1a;環境變量沒有配置好。解決辦法&#xff1a;配置環境變量&#xff1a;JAVA_HOME C:\Program Files\Java\jdk1.6.0_03就可以解決問題了本文轉自 yuwenhu 51CTO博客…

js寫法【3】

var m[];m[m.length]xx;//相當于push 對Repeat方法提供了8種寫法做比較&#xff0c;這一點還是不錯的。轉載于:https://www.cnblogs.com/baozhu/p/4862242.html

Eclipse設置控制臺日志輸出位置

1、選擇服務器配置 2、設置輸出文件路徑 轉載于:https://www.cnblogs.com/Neil223/p/5759693.html

現代制造工程——第七章(軋制和鍛造)

考點基于書本以及PPT的題目 一、軋制 二、鍛造

圖像邊緣梯度的定義

http://blog.csdn.net/u014020344/article/details/50196635

Jquery_JQuery之DataTables強大的表格解決方案

1、DataTables的默認配置 $(document).ready(function() { $(‘#example’).dataTable(); } ); 示例&#xff1a;http://www.guoxk.com/html/DataTables/Zero-configuration.html 2、DataTables的一些基礎屬性配置 “bPaginate”: true, //翻頁功能 “bLengthChange”: true, /…

內存管理1retain和release

Student.h: #import <Foundation/Foundation.h> interface Student : NSObject property int age; //默認會生成一個_age屬性 end Student.m: #import "Student.h" implementation Student //synthesize age_age;//xcode4.5中可以不使用synthesise方法&a…

目標檢測的圖像特征提取之(一)Hog特征提取

Hog特征實質是&#xff1a;梯度的統計信息,即針對邊緣作特征提取 意義&#xff1a;目標的表象和形狀 轉載于zouxy09大神的文章&#xff0c;加上自己些微的理解和應用&#xff01; http://blog.csdn.net/zouxy09/article/details/7929348/ 上述基本講清楚了&#xff0c;其實…

redis類型[string 、list 、 set 、sorted set 、hash]

1. Keys redis本質上一個key-value db&#xff0c;所以我們首先來看看他的key. 首先key也是字符串類型&#xff0c;但是key中不能包括邊界字符&#xff1b;由于key不是binary safe的字符串&#xff0c;所以像"my key"和"mykey\n"這樣包含空格和換行的key是…

matlab simulink筆記02——延遲模塊delay與單位延遲模塊unit delay

延遲模塊 單位延遲模塊 延遲模塊具有復位功能,當滿足復位條件時會進行復位操作,即輸出的值會恢復到初始值,而單位延遲模塊沒有復位功能; 延遲模塊的步長是可以設置的,而單位延遲模塊的步長固定為1,不可以改變

局域網傳輸速度升級

現在很多單位都建成了企業內部局域網&#xff0c;一般的企業網絡大多是使用雙 絞線連接網卡的方式來進行通信的。其中雙絞線通常采用的都是5類線&#xff0c;傳輸速率為100MB。而網卡則有一定的區別&#xff0c;很多網卡都是采取 10Mbps/100Mbmps自適應的網卡&#xff0c;即傳輸…

數據結構--棧 codevs 1107 等價表達式

codevs 1107 等價表達式 2005年NOIP全國聯賽提高組 時間限制: 1 s空間限制: 128000 KB題目等級 : 鉆石 Diamond題目描述 Description明明進了中學之后&#xff0c;學到了代數表達式。有一天&#xff0c;他碰到一個很麻煩的選擇題。這個題目的題干中首先給出了一個代數表達式&am…

目標檢測的圖像特征提取之(二)LBP特征

LBP特征實質是&#xff1a;圖像局部特征的提取 意義&#xff1a;紋理的提取 http://blog.csdn.net/zouxy09/article/details/7929531 1&#xff09;首先將檢測窗口劃分為1616的小區域&#xff08;cell&#xff09;&#xff1b; &#xff08;2&#xff09;對于每個cell中的一個…

VS2010安裝幫助文檔出現錯誤

安裝VS2010后的幫助文檔安裝出現錯誤:未能在指定文件夾中創建本地存儲區 安裝完VS2010后&#xff0c;出現錯誤&#xff0c;取消后 再安裝MSDN 打開“Help Library 管理器 - Microsoft Help 查看器 1.0” 提示“請為本地內容選擇位置” 默認的位置是在“C:\Documents and Settin…

matlab smulink筆記03——過零檢測

★過零檢測 變步長解算方法動態地評估計算下一個采樣時刻所使用的步長&#xff0c;當前后兩個采 樣點的狀態值變化大時&#xff0c;則縮小采樣步長&#xff0c;當前后兩個采樣點的值變化小時則增大步 這種做法使得解算器在計算不連續臨近區域時使用較小的步長&#xff0c;因為不…