0

I have an entity called User with two many-to-many relationships: User -mtm - Role and User -mtm - Course

public class User {

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "users_roles",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")})
    private List<Role> userRoles;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "users_courses",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "course_id", referencedColumnName = "id")})
    private List<Course> orderedCourses;
}

In Course entity:

public class Course  {

    @ManyToMany(mappedBy = "orderedCourses")
    private List<User> participants;

}

It looks similar in the Role entity.

When user with e.g. two roles assigned enrolls himself to some course (means that course is added to his orderedCourses list), he gets this course twice. So the user with two roles gets registered for the same course twice, user with 3 roles gets it three times etc.

It is noticeable in the junction table in the database. (one user has the same course few times which is unacceptable).

Looks like one ManyToMany relationship has an impact on another. But I don't know what is wrong. Everything is persisted to MySQL database by Hibernate (via Spring Data JPA)

user3541916
  • 183
  • 2
  • 10
  • 1. where is the code reproducing that problem? 2. Why isn't there a primary key constraint in the join table that would make this situation impossible in the first place? – JB Nizet Feb 18 '16 at 14:31
  • 1. Well, I've noticed those ManyToMany relationships cause the problem. 2. Could you be more precise and give some hint how to implement that? – user3541916 Feb 18 '16 at 14:53
  • 1
    1. That still doesn't tell me what your code is. There is a 0.001% chance that it is a Hibernate bug, and I have access to the Hibernate code. There is a 99.999% chance that it is a bug in your code, but I have no idea what it is. 2. http://stackoverflow.com/questions/5132923/how-to-add-a-primary-key-to-a-mysql-table – JB Nizet Feb 18 '16 at 14:57
  • 1. I save entities using .save() method from CRUD repository (both for Course and User). I used many combinations and noticed that there is some improper depedence between thoese two ManyToMany relationships. 2. I use hibernate.hbm2ddl.auto = create; this is production environment, can I achieve the same effect using just hibernate, not MySQL? – user3541916 Feb 18 '16 at 16:18
  • @user3541916 Please, add code, which shows how do you save it exactly, to your question. I test your mapping and everything is correct ( I use `Set` or lazy because of "multiple bags" exception using `Session`) – v.ladynev Feb 18 '16 at 16:52
  • Are you **absolutely sure** the duplicate records actually exist in the database and not only in the model once you have reloaded an entity. See here: http://stackoverflow.com/questions/1995080/hibernate-criteria-returns-children-multiple-times-with-fetchtype-eager – Alan Hay Feb 18 '16 at 16:57
  • @AlanHay Yes, I'm sure. I see those records in MySQL join table, there are repeated records. – user3541916 Feb 18 '16 at 17:26
  • I changed `List` to `Set` and now it is fine. I think `Set` is more desirable than `List`, but is it a real solution? It still doesn't clarify why te Course has been added many times. Should I add some code? I have a service with method that assigns Courses to User and then this method is called in Spring controller (data sent via POST). In the same controller I add some example data (courses, roles, users) in `@PostConstruct ` method. So should I paste only service method which is responsible for assigning courses to users, or everything? – user3541916 Feb 18 '16 at 17:52

0 Answers0