Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring + JPA many to many relationship
    primarykey
    data
    text
    <p>I'm very new to Spring and I'm trying to make a many-to-many relationship work as I expect. The relationships works fine, the tables are created and the data is inserted correctly. What I expect, is that when I empty a List (i.e. I empty the ArrayList "users" from an object of type "Group"), I expect the system to remove the relationships from the database - but it doesn't.</p> <p>I have the following classes:</p> <pre><code>@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&lt;User&gt; users = new ArrayList&lt;User&gt;(); ... } @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&lt;Group&gt; groups = new ArrayList&lt;Group&gt;(); ... } </code></pre> <p>Here are the DAOs:</p> <pre><code>@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; } } </code></pre> <p>Here is the test method:</p> <pre><code>@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! } </code></pre> <p>Am I doing something wrong or is it something not possible to do?</p> <p>Any hint is really appreciated.</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload