JPA @ManyToMany on only one side?
- by Ethan Leroy
I am trying to refresh the @ManyToMany relation but it gets cleared instead...
My Project class looks like this:
@Entity
public class Project {
...
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "PROJECT_USER",
joinColumns = @JoinColumn(name = "PROJECT_ID", referencedColumnName = "ID"),
inverseJoinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "ID"))
private Collection<User> users;
...
}
But I don't have - and I don't want - the collection of Projects in the User entity.
When I look at the generated database tables, they look good. They contain all columns and constraints (primary/foreign keys).
But when I persist a Project that has a list of Users (and the users are still in the database), the mapping table doesn't get updated gets updated but when I refresh the project afterwards, the list of Users is cleared.
For better understanding:
Project project = ...; // new project with users that are available in the db
System.out.println(project getUsers().size()); // prints 5
em.persist(project);
System.out.println(project getUsers().size()); // prints 5
em.refresh(project);
System.out.println(project getUsers().size()); // prints 0
So, how can I refresh the relation between User and Project?