0

我有一个 ValuedCustomer 和 Order,我想在 hibernate 中建立双向关系。我该怎么做呢?谢谢

@Entity
@Table(name="VCUST")
@DiscriminatorValue("VCUST")
@PrimaryKeyJoinColumns({
    @PrimaryKeyJoinColumn(name="CUST_ID",referencedColumnName="ID"),
    @PrimaryKeyJoinColumn(name="CUST_TYPE",referencedColumnName="TYPE")
})
public class ValuedCustomer extends Customer { 

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL,orphanRemoval = true)

    @JoinColumn(name = "customer_order", nullable = true)

    private Set<Order> orders = new HashSet<>();

}


@Entity
@Table(name=“Order”)
public class Order { 

@Id
private int id;
}
4

1 回答 1

0

这很容易。重要的是 @OneToMany 中的 mappedBy 属性,它指示 realtionship 由 Order 实体管理。

@Entity
@Table(name="VCUST")
@DiscriminatorValue("VCUST")
@PrimaryKeyJoinColumns({
    @PrimaryKeyJoinColumn(name="CUST_ID",referencedColumnName="ID"),
    @PrimaryKeyJoinColumn(name="CUST_TYPE",referencedColumnName="TYPE")
})
public class ValuedCustomer extends Customer { 

   @OneToMany(mappedBy= 'valuedCustomer" fetch = FetchType.LAZY, cascade = CascadeType.ALL,orphanRemoval = true)
   private Set<Order> orders = new HashSet<>();

}

@Entity
@Table(name=“Order”)
public class Order { 

   @Id
   private int id;

   @ManyToOne
   private ValuedCustomer valuedCustomer;


}
于 2019-06-07T12:19:48.743 回答