Hibernate逍遥游记-第5章映射一对多-02双向(set、key、one-to-many、inverse、cascade=all-delete-orphan)...
1.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 <class name="mypack.Monkey" table="MONKEYS"> 8 9 <id name="id" type="long" column="ID"> 10 <generator class="increment"/> 11 </id> 12 13 <property name="name" type="string" column="NAME" /> 14 15 <many-to-one 16 name="team" 17 column="TEAM_ID" 18 class="mypack.Team" 19 cascade="save-update" 20 /> 21 22 </class> 23 24 </hibernate-mapping>
2.
1 package mypack; 2 3 public class Monkey { 4 5 6 private long id; 7 private String name; 8 private Team team; 9 10 public Monkey() {} 11 12 public Monkey(String name, Team team) { 13 this.name = name; 14 this.team = team; 15 } 16 17 public long getId() { 18 return this.id; 19 } 20 21 public void setId(long id) { 22 this.id = id; 23 } 24 public String getName() { 25 return this.name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 public Team getTeam() { 32 return this.team; 33 } 34 35 public void setTeam(Team team) { 36 this.team = team; 37 } 38 }
3.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 <class name="mypack.Team" table="TEAMS" > 8 <id name="id" type="long" column="ID"> 9 <generator class="increment"/> 10 </id> 11 12 <property name="name" type="string" column="NAME" /> 13 <!-- 14 <set 15 name="monkeys" 16 cascade="all-delete-orphan" 17 inverse="true" 18 > 19 20 <key column="TEAM_ID" /> 21 <one-to-many class="mypack.Monkey" /> 22 </set> 23 --> 24 <set 25 name="monkeys" 26 inverse="true" 27 cascade="save-update" 28 > 29 30 <key column="TEAM_ID" /> 31 <one-to-many class="mypack.Monkey" /> 32 </set> 33 34 </class> 35 </hibernate-mapping>
4.
1 package mypack; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Team{ 7 8 private long id; 9 private String name; 10 private Set monkeys = new HashSet(); 11 12 public Team() {} 13 14 public Team(String name, Set monkeys) { 15 this.name = name; 16 this.monkeys = monkeys; 17 } 18 19 public long getId() { 20 return this.id; 21 } 22 23 public void setId(long id) { 24 this.id = id; 25 } 26 public String getName() { 27 return this.name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 public Set getMonkeys() { 34 return this.monkeys; 35 } 36 37 public void setMonkeys(Set monkeys) { 38 this.monkeys = monkeys; 39 } 40 }
5.
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-configuration 3 PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="dialect"> 8 org.hibernate.dialect.MySQLDialect 9 </property> 10 <property name="connection.driver_class"> 11 com.mysql.jdbc.Driver 12 </property> 13 <property name="connection.url"> 14 jdbc:mysql://localhost:3306/sampledb 15 </property> 16 <property name="connection.username"> 17 root 18 </property> 19 <property name="connection.password"> 20 1234 21 </property> 22 <property name="show_sql">true</property> 23 <mapping resource="mypack/Team.hbm.xml" /> 24 <mapping resource="mypack/Monkey.hbm.xml" /> 25 </session-factory> 26 </hibernate-configuration>
6.
1 package mypack; 2 3 import org.hibernate.*; 4 import org.hibernate.cfg.Configuration; 5 import java.util.*; 6 7 public class BusinessService{ 8 public static SessionFactory sessionFactory; 9 private Long idOfTom; 10 private Long idOfBULL; 11 private Long idOfJack; 12 private Long idOfDREAM; 13 14 static{ 15 try{ 16 Configuration config = new Configuration(); 17 config.configure(); 18 sessionFactory = config.buildSessionFactory(); 19 }catch(RuntimeException e){e.printStackTrace();throw e;} 20 } 21 22 public void printMonkeysOfTeam(Long teamId){ 23 Session session = sessionFactory.openSession(); 24 Transaction tx = null; 25 try { 26 tx = session.beginTransaction(); 27 Team team=(Team)session.get(Team.class,teamId); 28 printMonkeys(team.getMonkeys()); 29 tx.commit(); 30 }catch (RuntimeException e) { 31 if (tx != null) { 32 tx.rollback(); 33 } 34 throw e; 35 } finally { 36 session.close(); 37 } 38 } 39 40 public void saveTeamAndMonkeyWithCascade(){ 41 Session session = sessionFactory.openSession(); 42 Transaction tx = null; 43 try { 44 tx = session.beginTransaction(); 45 46 Team team=new Team("BULL",new HashSet()); 47 Monkey monkey=new Monkey(); 48 monkey.setName("Tom"); 49 50 monkey.setTeam(team); 51 team.getMonkeys().add(monkey); 52 53 session.save(team); 54 tx.commit(); 55 56 idOfBULL=team.getId(); 57 idOfTom=monkey.getId(); 58 59 }catch (RuntimeException e) { 60 if (tx != null) { 61 tx.rollback(); 62 } 63 e.printStackTrace(); 64 } finally { 65 session.close(); 66 } 67 } 68 69 public void associateTeamAndMonkey(){ 70 Session session = sessionFactory.openSession(); 71 Transaction tx = null; 72 try { 73 tx = session.beginTransaction(); 74 Team team=(Team)session.load(Team.class,idOfDREAM); 75 Monkey monkey=(Monkey)session.load(Monkey.class,idOfJack); 76 monkey.setTeam(team); 77 team.getMonkeys().add(monkey); 78 tx.commit(); 79 }catch (RuntimeException e) { 80 if (tx != null) { 81 tx.rollback(); 82 } 83 e.printStackTrace(); 84 } finally { 85 session.close(); 86 } 87 } 88 89 public void saveTeamAndMonkeySeparately(){ 90 Session session = sessionFactory.openSession(); 91 Transaction tx = null; 92 try { 93 tx = session.beginTransaction(); 94 95 Team team=new Team("DREAM",new HashSet()); 96 Monkey monkey=new Monkey(); 97 monkey.setName("Jack"); 98 99 session.save(team); 100 session.save(monkey); 101 102 tx.commit(); 103 idOfDREAM=team.getId(); 104 idOfJack=monkey.getId(); 105 106 }catch (RuntimeException e) { 107 if (tx != null) { 108 tx.rollback(); 109 } 110 e.printStackTrace(); 111 } finally { 112 session.close(); 113 } 114 } 115 116 public void deleteTeam(Long teamId){ 117 Session session = sessionFactory.openSession(); 118 Transaction tx = null; 119 try { 120 tx = session.beginTransaction(); 121 Team team=(Team)session.load(Team.class,teamId); 122 session.delete(team); 123 tx.commit(); 124 125 }catch (RuntimeException e) { 126 if (tx != null) { 127 tx.rollback(); 128 } 129 e.printStackTrace(); 130 } finally { 131 session.close(); 132 } 133 } 134 135 public void removeMonkeyFromTeam(Long teamId){ 136 Session session = sessionFactory.openSession(); 137 Transaction tx = null; 138 try { 139 tx = session.beginTransaction(); 140 Team team=(Team)session.load(Team.class,teamId); 141 Monkey monkey=(Monkey)team.getMonkeys().iterator().next(); 142 143 //解除team和Monkey的关联关系 144 team.getMonkeys().remove(monkey); 145 monkey.setTeam(null); 146 tx.commit(); 147 148 }catch (RuntimeException e) { 149 if (tx != null) { 150 tx.rollback(); 151 } 152 e.printStackTrace(); 153 } finally { 154 session.close(); 155 } 156 } 157 158 public void printMonkeys(Set monkeys){ 159 for (Iterator it = monkeys.iterator(); it.hasNext();) { 160 Monkey monkey=(Monkey)it.next(); 161 System.out.println("Monkeys in "+monkey.getTeam().getName()+ " :"+monkey.getName()); 162 } 163 } 164 165 public void saveTeamAndMonkeyWithInverse(){ 166 saveTeamAndMonkeySeparately(); 167 associateTeamAndMonkey(); 168 } 169 public void test(){ 170 171 saveTeamAndMonkeyWithCascade(); 172 saveTeamAndMonkeyWithInverse(); 173 printMonkeysOfTeam(idOfBULL); 174 //deleteTeam(idOfDREAM); 175 removeMonkeyFromTeam(idOfBULL); 176 } 177 178 public static void main(String args[]){ 179 new BusinessService().test(); 180 sessionFactory.close(); 181 } 182 }
转载于:https://www.cnblogs.com/shamgod/p/5296825.html
Hibernate逍遥游记-第5章映射一对多-02双向(set、key、one-to-many、inverse、cascade=all-delete-orphan)...相关推荐
- Hibernate逍遥游记-第2章-使用hibernate.properties
1. 1 package mypack; 2 3 import org.hibernate.*; 4 import org.hibernate.cfg.Configuration; 5 import ...
- 06章 映射一对多双向关联关系、以及cascade、inverse属性
当类与类之间建立了关联,就可以方便的从一个对象导航到另一个对象.或者通过集合导航到一组对象.例如: 对于给定的Emp对象,如果想获得与它关联的Dept对象,只要调用如下方法 Dept dept=emp ...
- java逍遥游记 下载_Hibernate逍遥游记 孙卫琴著 PDF下载
<Java开发专家•Hibernate逍遥游记(附CD-ROM光盘1张)>Hibernate是非常流行的对象一关系映射工具.<Java开发专家•Hibernate逍遥游记(附CD-R ...
- 《Java逍遥游记》
<Java逍遥游记>精彩章节下载 序 转眼之间,我在Java领域从事了八年多的创作了.在这八年中,我在Java的各个领域里遨游驰骋,既要探索各种技术的宏观架构和运用技巧,又要细细品味每种技 ...
- hibernate映射一对多双向关联关系实例
在电子商务应用中,经常会有这样的需求:根据给定的客户,得到该客户的所有订单:根据给定的订单,得到该订单的所属客户.对于这种双向关联的情况,在Hibernate应用中,也有人叫多对一双向关联,只是叫法不 ...
- (转)Hibernate关联映射——一对多(多对一)
http://blog.csdn.net/yerenyuan_pku/article/details/70152173 Hibernate关联映射--一对多(多对一) 我们以客户(Customer)与 ...
- Hibernate第四篇【集合映射、一对多和多对一】
前言 前面的我们使用的是一个表的操作,但我们实际的开发中不可能只使用一个表的-因此,本博文主要讲解关联映射 集合映射 需求分析:当用户购买商品,用户可能有多个地址. 数据库表 我们一般如下图一样设计数 ...
- hibernate annotation注解方式来处理映射关系
2019独角兽企业重金招聘Python工程师标准>>> 在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟 ...
- hibernate 映射表_Hibernate多对多映射-连接表
hibernate 映射表 Today we will look into Hibernate Many to Many Mapping using XML and annotation config ...
- 【Hibernate步步为营】--详解基本映射
Hibernate进行了分类整合发现其实Hibernate分为三大部分:核心对象.映射.HQL,这三大部分开发过程中最常使用,前几篇讨论了核心对象及对象之间的转换方法,接下来讨论Hibernate的映 ...
最新文章
- Windows下Hadoop的环境安装[转]
- 复旦大学教授邱锡鹏:NLP 任务中有哪些巧妙的 idea?
- 某平台的一次简单渗透测试记录
- NeuralBuild-用于Java、C#、PHP的DAO产生器
- python -- 三元运算符
- 【python】详解类class的继承、__init__初始化、super方法
- 信息学奥赛一本通C++语言——1041:奇偶数判断
- Java基础-this关键字和构造方法(10)
- 进程间同步的几种方法
- HibernateTemplate使用方法
- 神州信息盘活农村三资,以金融科技服务三农助推乡村振兴
- 疾病研究:我是一名34岁的进行性肌营养不良症患者
- 分享一个返利系统源码,前端uni+后端php开发的影票返利系统源码
- 【慧河网络安全组】Web基础和http协议培训题_1
- JAVA 将一个对象的所有字段值 赋给另一个 对象
- python 描边,吊打Pyecharts,这个新Python绘图库也可以这么漂亮!
- Linux---笔记总结
- AForge学习笔记(5):AForge.Imaging(下)
- Ruby On Rails-2.0.2源代码分析(1)-Rails的启动
- 纯css3制作简易钟表时钟
热门文章
- NYOJ759 你知道这个规律吗
- 计算机电源的瓦数是什么,电脑的电源功率大小有区别吗?对电脑有什么影响吗?...
- php本地文件包含 截断,php本地文件包含远程文件包含
- python入门之运算符的使用第一关_python基础教程之python 学习第四天|python基础教程|python入门|python教程...
- w ndows10怎么没体验指数,Win10没有“Windows体验指数”功能怎么进行系统评分【图文】...
- java技术架构选型方案报告.pdf,来啦,2020开源报告!
- shell逻辑运算符优先级_linux shell 逻辑运算符、逻辑表达式详解
- kotlin和python哪个好_对比 Go 语言,Kotlin 有什么优势和劣势?
- 马尔科夫决策过程(MDP) : BlackJack (MC-Off Policy)
- 算法:Longest Valid Parentheses(最长有效的括号)