Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate @ManyToMany delete relation
    text
    copied!<p>I have 2 entities: User and UsersList.</p> <pre><code>@Entity @Table(name = "USERS") public class User { @Id @GeneratedValue @Column(name = "ID") private Long id; @ManyToMany(cascade = CascadeType.REMOVE, mappedBy = "users") private List&lt;UsersList&gt; usersLists = new ArrayList&lt;UsersList&gt;(); public List&lt;UsersList&gt; getUsersLists() { return usersLists; } public void setUsersLists(List&lt;UsersList&gt; usersLists) { this.usersLists = usersLists; } } </code></pre> <p>and</p> <pre><code>@Entity @Table(name = "USERS_LIST") public class UsersList { @Id @GeneratedValue @Column(name = "ID") private Long id; @ManyToMany private List&lt;User&gt; users = new ArrayList&lt;User&gt;(); public List&lt;User&gt; getUsers() { return users; } public void setUsers(List&lt;User&gt; users) { this.users = users; } } </code></pre> <p>and code like this one:</p> <pre><code>// 1 List&lt;User&gt; dbUsers; // 3 the user instances are persisted in DB UsersList usersList = new UsersList(); usersList.setUsers(dbUsers); // 2 - now persist the usersList using hibernate... saveWithHibernate(usersList); //3 - delete using a persisted user deleteWithHibernate(dbUsers.get(0).getId()); </code></pre> <p>where </p> <pre><code>deleteWithHibernate(Long id) { User usr = hibernateTemplate.get(User.class, id); hibernateTemplate.delete(usr); } </code></pre> <p>In step 1 I have 3 rows in the USERS (USER_ID) table.</p> <p>After step 2 (second comment) I have 1 row in the USERS_LIST (USERS_LIST_ID) table and into the join table USERS_LIST_USERS (USER_ID, USERS_LIST_ID) 3 rows. </p> <p>What I want to achieve in step 3 is the following: when I delete one user from the table USERS - let's say user with USER_ID = 4, I want just the row with USER_ID = 4 from the join table to be deleted, and the others to remain.</p> <p>Is there an annotation solution to my problem?</p>
 

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