3

I have the following class:

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;

I do not have any relational mappings in User as I don't think I need this to be bidirectional.

Whenever I try to delete an instance of game, I get this error:

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".

I have tried all sorts of combinations of LAZY/EAGER and different Cascading types and cannot figure it out. There are several other posts on here, but I don't want the User to be deleted, just the row from the games_attendees table.

*Update:*After troubleshooting for a while, I decided to try executing a native query before my delete statement that just deletes all rows from the games_attendees table (the join table) where the game_ids match. This produces the desired result but it seems to be an anti-JPA way to go about it. Is there a better way?

eliot
  • 1,319
  • 1
  • 14
  • 33

0 Answers0