Spring + JPA for many relationships

I am very new to Spring, and I try to do many-to-many work as I expect. Relations work fine, tables are created, and data is inserted correctly. What I expect is that when I empty the List (that is, I release the ArrayList users from the Group object), I expect the system to remove the relationships from the database, but this is not the case.

I have the following classes:

@Entity @Table(name = "`group`") public class Group { @Id @GeneratedValue @Column(name = "id") private int id; @Column(name = "name") private String name; @ManyToMany(cascade = {CascadeType.ALL}) @JoinTable( name = "`user_has_group`", joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id") ) private List<User> users = new ArrayList<User>(); ... } @Entity @Table(name = "`user`") public class User { @Id @GeneratedValue @Column(name = "id") private int id; @Column(name = "name") private String name; @ManyToMany(mappedBy = "users") private List<Group> groups = new ArrayList<Group>(); ... } 

Here is the DAO:

 @Repository public class GroupJpaDao implements GroupDao { private EntityManager em; @Transactional public void save(Group group) { this.em.merge(group); } ... @PersistenceContext void setEntityManager(EntityManager entityManager) { this.em = entityManager; } } @Repository public class UserJpaDao implements UserDao { private EntityManager em; @Transactional public void save(User user) { this.em.merge(user); } ... @PersistenceContext void setEntityManager(EntityManager entityManager) { this.em = entityManager; } } 

Here is a test method:

 @Test public void test() { Group g = new Group(); g.setName("Test group"); groupDao.save(g); // Works fine - inserts the group into the database User u = new User(); u.setName("Test user"); userDao.save(u); // Works fine - inserts the user into the database g.addUser(u); groupDao.save(g); // Works fine - adds the many-to-many relationship into the database g.removeAllUsers(); groupDao.save(g); // Doesn't work - I'm expecting it to remove all the related relationships from the database but it doesn't! } 

Am I doing something wrong or is it impossible to do?

Any tips are really appreciated.

Thanks!

+6
source share
2 answers

I am re-reading your question and now the answer is clear. Create a group g and save it. But since your save method uses merge , and you don't take into account the value returned by merge to assign it to g , you continue to merge the transition group g, which never has an identifier. Thus, every time a merge is called, you are actually creating a new group, rather than modifying a previously created one.

Change the save method to

 public Group save(Group group) { return this.em.merge(group); } 

and always reassign the result to g :

 g = groupDao.save(g); 

Of course, the same should be done for the user.

+2
source

you remove users from the group, which is not enough. you need to remove all users from the group and remove this group from the list of all these groups.

0
source

Source: https://habr.com/ru/post/905847/


All Articles