zoukankan      html  css  js  c++  java
  • 两个表相关联如何创建实体

    • 创建表:
    1. t_customer  
      -- Create table
      create table MISPPRD.T_CUSTOMER
      (
        CUSTOMER_ID     NUMBER(20) not null,
        ADDRESS         NUMBER(20)
      )
      -- Add comments to the table 
      comment on table MISPPRD.T_CUSTOMER
        is '客户信息表';
      -- Add comments to the columns 
      comment on column MISPPRD.T_CUSTOMER.CUSTOMER_ID
        is '客户ID';
      comment on column MISPPRD.T_CUSTOMER.ADDRESS
        is '地址';
      -- Create/Recreate primary, unique and foreign key constraints  
      
        alter table MISPPRD.T_CUSTOMER
        add constraint PK_T_CUSTOMER__CUSTOMER_ID primary key (CUSTOMER_ID)
        
        alter table MISPPRD.T_CUSTOMER
        add constraint FK_CUSTOMER__CURRENT_ADDRESS foreign key (ADDRESS)
        references MISPPRD.T_CUSTOMER_ADDRESS (ADDRESS_ID);
        
    2. T_CUSTOMER_ADDRESS
      -- Create table
      create table MISPPRD.T_CUSTOMER_ADDRESS
      (
        ADDRESS_ID  NUMBER(20) not null,
        CUSTOMER_ID NUMBER(20) not null,
        DETAIL      VARCHAR2(511)
      )
      -- Add comments to the table 
      comment on table MISPPRD.T_CUSTOMER_ADDRESS
        is '客户地址信息表';
      -- Add comments to the columns 
      comment on column MISPPRD.T_CUSTOMER_ADDRESS.ADDRESS_ID
        is '地址ID';
      comment on column MISPPRD.T_CUSTOMER_ADDRESS.CUSTOMER_ID
        is '客户ID';
      comment on column MISPPRD.T_CUSTOMER_ADDRESS.DETAIL
        is '详细地址';
      -- Create/Recreate primary, unique and foreign key constraints 
        alter table MISPPRD.T_CUSTOMER_ADDRESS
        add constraint PK_T_CUSTOMER_ADDRESS__ID primary key (ADDRESS_ID)

      观察两个表的关联关系,易得 : t_customer与t_customer_address的关系为一对多,t_customer_address与t_customer的关系为多对一

    • 创建实体: 
    1.  CustomerAddress
    @Entity
    @Table(name = "T_CUSTOMER_ADDRESS")
    @org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
    public class CustomerAddress {
    
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customer_address_seq")
        @SequenceGenerator(name = "customer_address_seq", sequenceName = "SEQ_CUST_ADDRESS__ADDRESS_ID", allocationSize = 1)
        @Column(name = "ADDRESS_ID", nullable = false)
        private Long addressId;
    
    
        @ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,
                CascadeType.MERGE }, optional = true)
        @JoinColumn(name = "CUSTOMER_ID")
        private Customer customer;
    
      public Long getAddressId() {
            return addressId;
        }
    
        public void setAddressId(Long addressId) {
            this.addressId = addressId;
        }
      public Customer getCustomer() {
            return customer;
        }
    
        public void setCustomer(Customer customer) {
            this.customer = customer;
        }
    } 
    • 2. Customer
    @Entity
    @Table(name = "T_CUSTOMER")
    @org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
    public class Customer {
     
       @Id @GeneratedValue(strategy
    = GenerationType.SEQUENCE, generator = "customer_seq") @SequenceGenerator(name = "customer_seq", sequenceName = "SEQ_CUSTOMER__CUSTOMER_ID", allocationSize = 1) @Column(name = "CUSTOMER_ID", nullable = false) private Long customerId;
      @OneToMany(cascade
    = { CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "customer") private List<CustomerAddress> address;   

      public Long getCustomerId() { return customerId;
      }  
      public void setCustomerId(Long customerId) {
        this.customerId = customerId;
      }
      
    public List<CustomerAddress> getAddress() { return address; } public void setAddress(List<CustomerAddress> address) { this.address = address; } }
  • 相关阅读:
    >动态规划 4.26
    树链剖分+线段树求路径交
    PTA团体程序设计天梯赛-练习集 L2 网红点打卡攻略(模拟)
    PTA团体程序设计天梯赛-练习集 L2完全二叉树的层序遍历(递归)
    PTA团体程序设计天梯赛-练习集 L3-020 至多删三个字符 (dp)
    codeforces1509 D. Binary Literature (构造+指针)
    函数内容小结
    关于vim复制剪贴粘贴命令的总结-转
    GCC编译命令常用选项
    Ubuntu 和 windows1下文件夹共享的指令
  • 原文地址:https://www.cnblogs.com/yinyl/p/8006357.html
Copyright © 2011-2022 走看看