@Entity
public class Role {
...
@ManyToMany
@JoinTable( name = "user_has_role", joinColumns = { @JoinColumn( name = "role_fk" ) }, inverseJoinColumns = { @JoinColumn( name = "user_fk" ) } )
private Set<User> userCollection;
...
}
@Entity
public class User {
...
//bi-directional many-to-many association to Role
@ManyToMany( mappedBy = "userCollection" )
private Set<Role> roleCollection;
...
}
em.createQuery( "DELETE Role" ).executeUpdate();
this answer
for ( ... )
{
A a = aDao.getObject(aId);
B b = bDao.getObject(bId);
b.getAs().remove(a);
a.getBs().remove(b);
bDao.saveObject(b);
}
有没有一种方法可以在不遍历所有数据的情况下一次删除JoinTable中的所有关联?
可能有一个特殊的HQL命令,比如
DELETE Role.user_has_role
?