Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA Hibernate many-to-many cascading
    primarykey
    data
    text
    <p>I am using JPA 2.0 and hibernate. I have a User class and a Group class as follows:</p> <pre><code>public class User implements Serializable { @Id @Column(name="USER_ID") private String userId; @ManyToMany @JoinTable(name = "USER_GROUP", joinColumns = { @JoinColumn(name = "GROUP_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") } ) private Set&lt;Group&gt; groupList; //get set methods } public class Group { @Id @Column(name="GROUP_ID") private String groupId; @ManyToMany(mappedBy="groupList") private Set&lt;User&gt; memberList; //get set methods } </code></pre> <p>And then, I create a user and group and then assign the user to the group.</p> <p>What I want to have is when I delete the group, the group will be deleted (of course) and all the user-group relationship that the group has will be automatically deleted from the USER_GROUP join table but the user itself is not deleted from the USER table.</p> <p>With the code I have above, only the row in the GROUP table will be deleted when I delete a group and the user will still have an entry to the deleted group in the USER_GROUP join table.</p> <p>If I put cascade in the User class like this:</p> <pre><code>@ManyToMany(cascade=CascadeType.ALL) @JoinTable(name = "USER_GROUP", joinColumns = { @JoinColumn(name = "GROUP_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") }) private Set&lt;Group&gt; groupList; </code></pre> <p>When I delete the group, the user will be deleted as well!</p> <p>Is there any way to achieve what I want?</p>
    singulars
    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.
    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