所以我已经搞砸了一段时间,我似乎无法让它发挥作用。
因此,向集合中添加项目可以正常工作。但删除似乎根本不起作用。我的映射有什么问题?
父元素非常简单。子元素的主键由父ID和客户ID组成。在父协会中,客户只能添加一次。
非常感谢您提供的任何帮助。
代码:
@Entity
@Table(name = "PARENT_TABLE")
public class Parent {
private Integer parentId;
private List<Child> children;
@Id
@Column(name="PARENT_ID")
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent",orphanRemoval=true)
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Parent)) return false;
Parentt that = (Parent) o;
return Objects.equals(getParentId(), that.getParentId());
}
@Override
public int hashCode() {
return Objects.hash(getParentId());
}
}
@Entity
@Table(name = "CHILD_TABLE")
public class Child {
private Parent parent;
private Customer customer;
@Id
@ManyToOne
@JoinColumn(name="PARENT_ID")
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent parent;
}
@Id
@ManyToOne
@JoinColumn(name="CUSTOMER_ID")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Child)) return false;
Child that = (Child) o;
return Objects.equals(getParent(), that.getParent()) &&
Objects.equals(getCustomer(), that.getCustomer());
}
@Override
public int hashCode() {
return Objects.hash(getParent(), getCustomer());
}
}
@Entity
@Table(name = "CUSTOMER_TABLE")
public class Customer{
private Integer customerId;
@Id
@Column(name="CUSTOMER_ID")
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
}