4
假設我有兩個實體:組織和用戶。每個用戶都可以成爲許多組織的成員,並且每個組織都可以擁有許多用戶。如何從與JPA和休眠中的許多孩子的@ManyToMany關係中刪除子對象
@Entity
public class User {
@ManyToMany
Set<Organization> organizations;
//...
}
@Entity
public class Organization {
@ManyToMany(mappedBy="organizations")
Set<User> users;
//...
}
現在,我想刪除一個組織(假設它有1000個成員)。 當用戶有一些組織,這個代碼是確定:
void removeOrgFromUser(Integer userId,Integer orgId){
User user = session.load(User.class, userId);
for (Organization org : user.organizations) {
if(org.getId().equals(orgId))
user.organizations.remove(org);
}
session.update(user);
}
但是當組織數爲10000,這個解決方案沒有很好的表現。
我該如何解決?
從連接表中刪除行。 – Antoniossss
我想用hibernate或hql來做這件事, –
@Antoniossss with sql native? –