问题
I want UserAcounts to be able to have many UserGroups.And All Groups can have many Users.And there is a join table.I want the releation between useraccount and usergroup in join table to be deleted when a useraccount is deleted.
Actually i want to use "on delete cascade".In ManyToMany relation I coulndn't run it unfortunately.I've tried so much thing but no solution I've found.
Note:I just want Relation to be deleted with on delete cascade
is it possible is there a way to do that?
Here is my hibernate 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've researched too much but i couldn't run it.
回答1:
A cascade set to DELETE wouldn't delete the association between a group and a user when you delete a user. It would delete the groups themselves.
To delete the association, you just have to remove all the groups from the collection of groups of the user before deleting the user:
user.getUserGroups().clear();
session.delete(user);
Removing the groups from the users is what will delete the associations from the join table.
回答2:
The example from JB Nizet is great for deleting associations for several entities. I had to delete 300 000 associations. In that case, it might be preferable to use an SQLQuery even though it's not Hibernate friendly (and the fact that you use native SQL language is not pretty). Due to performance issues, it might be necessary.
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();
来源:https://stackoverflow.com/questions/8699153/on-delete-cascade-hibernate-manytomany