我有以下课程:
public String sport;
public String date;
public String time;
@ManyToOne
public Location location;
public int min, max;
// 0 - public, 1 - invite only
public int scope;
@ManyToOne
public User creator;
@ElementCollection
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "games_attendees")
public List<User> attendees;
@ElementCollection
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "games_subs")
public List<User> subs;
@ElementCollection
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "games_invitees")
public List<User> invitees;
我在User中没有任何关系映射,因为我认为我不需要这是双向的。
每当我尝试删除游戏实例时,都会收到此错误:
org.hibernate.exception.ConstraintViolationException: could not execute update query
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "games" violates foreign key constraint "fkc80e3afb4429c99e" on table "games_attendees"
Detail: Key (id)=(3) is still referenced from table "games_attendees".
我尝试了各种LAZY / EAGER和不同级联类型的组合,但无法弄明白。这里有几个其他帖子,但我不想删除用户,只是删除了games_attendees表中的行。
* 更新: *经过一段时间的故障排除后,我决定尝试在我的删除语句之前执行本机查询,该删除语句只删除game_idtendees表(连接表)中game_ids匹配的所有行。这产生了期望的结果,但它似乎是一种反JPA的方式。还有更好的方法吗?