多対多の関連付けを持つ次の 2 つのエンティティがあります。
@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();
この回答に示すように、「user_has_role」JoinTable のすべての関連付けをクリアする必要があります。
for ( ... )
{
A a = aDao.getObject(aId);
B b = bDao.getObject(bId);
b.getAs().remove(a);
a.getBs().remove(b);
bDao.saveObject(b);
}
すべてのデータを反復せずに、JoinTable 内のすべての関連付けを一度に削除する方法はありますか? たぶん、特別な HQL-Command のようなものがありDELETE Role.user_has_role
ますか?