On Cascade Hibernate ManyToMany

I want UserAcounts to have many UserGroups.And All groups can have many users. And there is a join.I table wants the exemption between useraccount and usergroup in the join table to be deleted when the user account is deleted.

Actually I want to use "on delete cascade". Regarding ManyToMany, I cannot run it, unfortunately. I tried so much but did not find the solution I found.

Note. I just need Relation to be deleted with delete cascade

Is it possible, is there a way to do this?

Here are my sleep classes

@SuppressWarnings("serial") @Entity @Table(name = "USER_ACCOUNT") public class UserAccount implements Serializable { @Id @Column(name = "ID") @GeneratedValue private Long id; @Column(name = "NAME") private String name; @Column(name = "SURNAME") private String surname; @Column(name = "EMAIL") private String email; @Column(name = "USER_NAME") private String username; @Column(name = "PASSWORD") private String password; @Column(name = "ENABLED") @Type(type = "yes_no") private boolean enabled; @Column(name = "ACCOUNT_NON_EXPIRED") @Type(type = "yes_no") private boolean accountNonExpired; @Column(name = "CREDENTIALS_NON_EXPIRED") @Type(type = "yes_no") private boolean credentialsNonExpired; @Column(name = "ACCOUNT_NON_LOCKED") @Type(type = "yes_no") private boolean accountNonLocked; @Column(name = "ENTRY_DATE") private Date entryDate; @Column(name = "UPDATE_DATE") private Date updateDate; @Column(name = "LAST_LOGIN_DATE") private Date lastLoginDate; @Column(name = "LOCAL") private String local; @ManyToMany(cascade = CascadeType.ALL,fetch=FetchType.EAGER) @JoinTable(name = "ACCOUNT_GROUP", joinColumns = { @JoinColumn(name = "ID") }, inverseJoinColumns = { @JoinColumn(name = "GROUP_ID") }) private List<UserGroup> userGroups; @SuppressWarnings("serial") @Entity @Table(name = "USER_GROUP") public class UserGroup implements Serializable { @Id @Column(name = "GROUP_ID") @GeneratedValue private Long id; @Column(name = "GROUP_NAME") private String name; @Column(name = "GROUP_DESCRIPTION") private String description; 

I explored too much, but I could not start it.

+2
source share
2 answers

The cascade set to DELETE does not remove the link between the group and the user when the user is deleted. He will delete the groups themselves.

To remove an association, simply remove all groups from the user group collection before deleting the user:

 user.getUserGroups().clear(); session.delete(user); 

Removing user groups is what leads to the removal of associations from the connection table.

+5
source

The JB Nizet example is great for removing associations for multiple objects. I had to remove 300,000 associations. In this case, it would be preferable to use SQLQuery, even if it does not support Hibernate (and the fact that you use the native SQL language is not very good). Due to performance issues, it may be required.

  SQLQuery queryDeleteDisabled = getSession().createSQLQuery("delete from ACCOUNT_GROUP where ID in (select ID from USER_ACCOUNT where ENABLED=?)"); queryDeleteDisabled.setParameter(0, false); int nbDelete = queryDeleteDisabled.executeUpdate(); 
0
source

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


All Articles