hibernate 中映射关系配置

多对多 :

外键维护权,一方放弃inverse="true",并且不放弃维护权的一方,加入 cascade="save-update":推荐方案

1 Student {
2         // 一个学生对应 多门课
3         // Set
4         Set<Cource> cources ; 
5 }
6     Cource {
7         // 一门课,多个学生
8         Set<Student> students ;
9     }
 1 <!-- 
 2             set:配置多对多
 3             private Set<Course> courses = new HashSet<Course>();
 4                 * set name:表示集合的属性名称
 5                 * table="":表示中间表的名称
 6                 * key column="":对应中间表中Studuent对应的外键列的名称
 7                 * many-to-many class="":表示集合属性名称对应的实体类型(全路径)
 8                 * column="":对应中间表中Course对应的外键列的名称
 9          -->
10         <set name="courses" table="t_s_c">
11             <key column="sid"></key>
12             <many-to-many class="cn.ibbidream.b_manyToMany.Course" column="cid"></many-to-many>
13         </set>
14 
15 
16 <!-- 
17             set:配置多对多
18             Set<Student> students = new HashSet<Student>();
19                 * set name:表示集合的属性名称
20                 * table="":表示中间表的名称
21                 * key column="":对应中间表中Course对应的外键列的名称
22                 * many-to-many class="":表示集合属性名称对应的实体类型(全路径)
23                 * column="":对应中间表中Student对应的外键列的名称
24          -->
25         <set name="students" table="t_s_c">
26             <key column="cid"></key>
27             <many-to-many class="cn.ibbidream.b_manyToMany.Student" column="sid"></many-to-many>
28         </set>

 

一对多 :

一般是在一方放弃。(从业务场景上来分析,一般先存1 方,再存多方,那么就存多方的时候建立关系就比较合理。)inverse="true"

1 Customer {
2         // 一个客户 多个订单 
3         Set<Order> orders ;
4 }
5     Order {
6         // 一个订单 一个客户 
7         Customer customer ;
8     }
 1 hbm 文件配置
 2 <!-- 
 3             多的一端的映射
 4             private Customer customer;
 5                 * name="":表示对象的属性名称
 6                 * class="":表示对象的属性名称的实体的全路径
 7                 * column="":表示外键列的名称
 8          -->
 9         <many-to-one name="customer" class="cn.ibbidream.a_oneToMany.Customer" column="cid"></many-to-one>
10 
11 <!-- 
12             set:一对多的映射:
13             Set<Order> orders = new HashSet<Order>();
14                * set name:集合的属性名称
15                * key column:多的一端的外键列的名称
16                * one-to-many class:集合中存放的对象的全路径
17         -->
18         <set name="orders">
19             <key column="cid"></key>
20             <one-to-many class="cn.ibbidream.a_oneToMany.Order"/>
21         </set>

 

一对一:

1 Company {
2         // 一个公司 一个地址
3         Address address;
4     }
5     Address  {
6         // 一个地址 对应一个公司
7         Company company ;
8     }

 

转载于:https://www.cnblogs.com/RedHat-Linux/p/7931894.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值