Chúng tôi có hai đối tượng sau đây với nhiều-nhiều hiệp hội:Làm thế nào để xóa tất cả các liên kết trong một Hibernate JoinTable cùng một lúc?
@Entity
public class Role {
...
@ManyToMany
@JoinTable(name = "user_has_role", joinColumns = { @JoinColumn(name = "role_fk") }, inverseJoinColumns = { @JoinColumn(name = "user_fk") })
private Set<User> userCollection;
...
}
và
@Entity
public class User {
...
//bi-directional many-to-many association to Role
@ManyToMany(mappedBy = "userCollection")
private Set<Role> roleCollection;
...
}
Nếu chúng ta muốn cắt toàn bộ dữ liệu với
em.createQuery("DELETE Role").executeUpdate();
chúng ta phải rõ ràng tất cả các liên kết trong "User_has_role" JoinTable như được hiển thị trong this answer:
for (...)
{
A a = aDao.getObject(aId);
B b = bDao.getObject(bId);
b.getAs().remove(a);
a.getBs().remove(b);
bDao.saveObject(b);
}
Có cách nào để xóa tất cả các liên kết trong JoinTable cùng một lúc mà không cần lặp qua tất cả dữ liệu không? Có thể có một lệnh HQL đặc biệt như DELETE Role.user_has_role
?