I have a ValuedCustomer and Order and I want to do a bidirectional relationship in hibernate. How do I do this? Thanks
@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;
}
That's easy. Important is the mappedBy attribute in @OneToMany that indicates that the realtionship is managed by the Order entity.
@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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.