3

我有以下课程:

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 和不同 Cascading 类型的各种组合,但无法弄清楚。这里还有其他几篇文章,但我不希望删除用户,只删除 games_attendees 表中的行。

*更新: *经过一段时间的故障排除后,我决定尝试在我的删除语句之前执行一个本机查询,该语句只删除 game_ids 匹配的 games_attendees 表(连接表)中的所有行。这会产生预期的结果,但它似乎是一种反 JPA 的方法。有没有更好的办法?

4

0 回答 0