Let's say I have two objects: Organization and User. Each user can be a member of many organizations, and each organization can have many users.
@Entity public class User { @ManyToMany Set<Organization> organizations;
Now I want to delete the organization (for example, it has 1000 members). When a user has few organizations, this code is in order:
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); }
But when the number of organizations is 10,000, this solution does not have good performance.
How can i fix this?
source share