【发布时间】:2018-10-23 22:53:44
【问题描述】:
我有 2 个实体
这是第一个实体
public class Manager {
// ...
@OneToMany(mappedBy = "manager", cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<ExpertAndRequest> requests;
// ...
}
第二个实体。这是两个实体的绑定表。
@Entity
@Data
@Table(name = "SOME_TABLE_NAME")
@IdClass(ExpertAndRequestId.class)
public class ExpertAndRequest implements Serializable {
@Id
private Long managerId;
@Id
private Long requestId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "managerId", updatable = false, insertable = false, referencedColumnName = "id")
private Manager manager;
@ManyToOne
@JoinColumn(name = "requestId", updatable = false, insertable = false, referencedColumnName = "id")
private ParticipantRequest request;
}
所以我从表中删除数据
this.managerRepository.delete(manager);
我得到了例外:
org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation
我做错了什么?
编辑:
我更新了上面的课程。
这是 IdClass
@Data
public class ExpertAndRequestId implements Serializable {
private long managerId;
private long requestId;
public int hashCode() {
return (int)(managerId + requestId);
}
public String toString() {
return String.format("ExpertAndRequestId [expert = \"%s\", request=\"%s\"]", this.managerId, this.requestId);
}
public boolean equals(Object object) {
if (object instanceof ExpertAndRequestId) {
ExpertAndRequestId otherId = (ExpertAndRequestId) object;
return (otherId.requestId == this.requestId) && (otherId.managerId == this.managerId);
}
return false;
}
}
第三个实体
public class ParticipantRequest {
// ...
@OneToMany(mappedBy = "request", cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<ExpertAndRequest> experts;
// ...
}
我从https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany获取的示例
【问题讨论】:
标签: java spring hibernate spring-data-jpa