Unidirectional Relationship in Entity-Bean (JPA)

后端 未结 2 1886
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 12:56

How to make Unidirectional Relationship in EJB 3.0 Entity-Beans (JPA)?

For example Customer know about Order but Order has not any method for Customer. using (@OneTo

2条回答
  •  甜味超标
    2021-01-05 13:33

    Here's how you'd make a unidirectional @OneToMany relationship using JPA 2.0:

    @Entity
    public class Customer {
      @Id
      @Column(name="cust_id")
      private long id;
      ...
      @OneToMany
      @JoinColumn(name="owner_id", referencedColumnName="cust_id")
      private List order;
      ...
    }
    
    @Entity
    public class Order {
        @Id
        @Column(name="order_id")
        private long id;
        ...
    }
    

    Relational database:

    Customer:

    +---------+---------+------+-----+---------+-------+
    | Field   | Type    | Null | Key | Default | Extra |
    +---------+---------+------+-----+---------+-------+
    | cust_id | int(11) | NO   | PRI | NULL    |       |
    +---------+---------+------+-----+---------+-------+
    

    Order:

    +----------+---------+------+-----+---------+-------+
    | Field    | Type    | Null | Key | Default | Extra |
    +----------+---------+------+-----+---------+-------+
    | order_id | int(11) | NO   | PRI | NULL    |       |
    | owner_id | int(11) | NO   | MUL | NULL    |       |
    +----------+---------+------+-----+---------+-------+
    

提交回复
热议问题