我有一个Response实体,每个响应只能有一个用户,每个用户都可以有很多响应。我正在尝试使用响应实体中的@JoinTable通过查找表连接它,如下所示:
create_engine(database_uri, echo=True)
但我无法删除用户。我收到以下错误:
@ManyToOne()
@JoinTable(
name = "response_user",
joinColumns = @JoinColumn(name = "response_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
@JsonIgnoreProperties({ "firstName", "lastName", "email", "activated", "regDate", "lastVisit" })
private User user;
我想要实现的是,当用户被删除时,它还会删除与该用户相关的所有内容。
答案 0 :(得分:1)
public class Response {
@ManyToOne
@JoinTable(name="response_user",
joinColumns={@JoinColumn(name="response_id")},
inverseJoinColumns={@JoinColumn(name="user_id")})
private User user;
}
您的用户实体应如下所示:
public class User {
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonIgnore
private Collection<Response> responses;
}