• 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     }
  • 相关阅读:
    Why does the memory usage increase when I redeploy a web application?
    lsof
    Advising controllers with the @ControllerAdvice annotation
    springMVC(一): 整体请求过程概述
    正则表达式30分钟入门教程
    Python基本语法_强制数据类型转换
    Python 正则表达式入门(初级篇)
    python实现简单爬虫功能
    在python3.3后urllib2已经不能再用,只能用urllib.request来代替
    JSON
  • 原文地址:https://www.cnblogs.com/RedHat-Linux/p/7931894.html
Copyright © 2020-2023  润新知