zoukankan      html  css  js  c++  java
  • Hibernate基数映射关系

    Hibernate基数映射关系

    一、基数映射——一对一关系映射

      每个账户对应一个地址的数据库关系为例:

    1、SQL脚本:
        -- 一对一(唯一外键)
        drop table t_addr;
        drop table t_act;
        create table t_act(
           id int primary key auto_increment,
           actNo varchar(50) not null unique,
           passwd varchar(50) not null,
           balance double
        );

        create table t_addr(
           id int primary key auto_increment,
           pro varchar(50) not null,
           city varchar(50) not null,
           aid int unique,
           foreign key(aid) references t_act(id)
        );

        select * from t_act;
        select * from t_addr;

    2、pojo源码创建
          1.Account类
        public class Account {
            private int id;
            private String actNo;
            private String passwd;
            private double balance;    
            private Address addr;//关联属性(体现外键--关系)
            public Account() {
                super();
            }
            public Account(String actNo, String passwd, double balance) {
                super();
                this.actNo = actNo;
                this.passwd = passwd;
                this.balance = balance;
            }
           //所有的get/set方法
        }

     2.Address类
        public class Address {
            private int id;
            private String pro;
            private String city;    
            private Account act;
            public Address() {
                super();
            }
            public Address(String pro, String city) {
                super();
                this.pro = pro;
                this.city = city;
            }
           //所有的get/set方法
        }

    3、配置文件

    1.Account.hbm.xml
        <hibernate-mapping>
           <class name="com.hibernate.one2one.Account" table="t_act">   
              <id name="id" >
            <generator class="native"></generator>
              </id>     
              <property name="actNo" unique="true" not-null="true"></property>
              <property name="passwd" not-null="true"></property>
              <property name="balance"></property>      
              <!-- 关联关系 -->      
              <one-to-one name="addr"
                      cascade="all"
                      property-ref="act"></one-to-one>
           </class>
        </hibernate-mapping>

    2.Address.hbm.xml
        <hibernate-mapping>
           <class name="com.hibernate.one2one.Address" table="t_addr">   
              <id name="id" >
            <generator class="native"></generator>
              </id>    
              <property name="pro"  not-null="true"></property>
              <property name="city"  not-null="true"></property>      
              <!-- 关联关系 -->
              <many-to-one name="act"
                        column="aid"
                        unique="true"
                        cascade="all"></many-to-one>
           </class>
        </hibernate-mapping>

    4、APP测试方法
        Session s = HibernateSessionFactory.getSession();
        Transaction tr = s.beginTransaction();        
        ......(略)
        s.delete(act);
        tr.commit();

    二、基数映射——一对多

      一个班级拥有多个学生为例:

    三、基数映射——多对多

      多个学生选课,每个学生可选多门课为例:

  • 相关阅读:
    win10一键访问更改适配器的方法
    windows server 2019 hyper-v+zabbix3.4
    Hyper-V 配置虚拟网络
    ros routeros pppoe一直拨号获取到不一样的IP为止。
    usb3.0 3.1 3.2的区别和联系
    西部数据绿盘、蓝盘、黑盘、红盘和紫盘的区别
    Ros-routeros winbox for win,mac,android,ios客户端大全
    《高效能人士的七个习惯》:好的时间管理,每天只做6件事
    快速了解C# 8.0中“可空引用类型(Nullable reference type)”语言特性
    Kubernetes中分布式存储Rook-Ceph的使用:一个ASP.NET Core MVC的案例
  • 原文地址:https://www.cnblogs.com/liuzhenyou/p/4691846.html
Copyright © 2011-2022 走看看