조직과 사용자라는 두 개의 엔터티가 있다고 가정 해 봅시다. 모든 사용자는 많은 조직의 구성원이 될 수 있으며 모든 조직은 많은 사용자를 보유 할 수 있습니다.JPA와 최대 절전 모드에서 많은 자식이있는 @ManyToMany 관계에서 자식 개체를 제거하는 방법
@Entity
public class User {
@ManyToMany
Set<Organization> organizations;
//...
}
@Entity
public class Organization {
@ManyToMany(mappedBy="organizations")
Set<User> users;
//...
}
이제 조직을 제거하고 싶습니다 (1000 명의 회원이 있다고 가정 해 보겠습니다). 사용자가 몇 단체가있는 경우 ,이 코드는 괜찮 : 조직 수는 10,000 때
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);
}
그러나,이 솔루션은 좋은 성능을 가지고 있지 않습니다.
어떻게 해결할 수 있습니까?
조인 테이블에서 행을 제거하십시오. – Antoniossss
hibernate 또는 hql을 사용하여이 작업을 수행하고 싶습니다. –
@Antoniossss with sql native? –