两个表相关联如何创建实体

  • 创建表:
  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; } }
原文地址:https://www.cnblogs.com/yinyl/p/8006357.html