简体   繁体   中英

Bulk Insert Many-to-Many Relationship

I have an entities defined as:

class Group {

    // some irrelevant fields

    @ManyToMany(targetEntity=User.class)
    private List<User> users;
}

class User {

    // some irrelevant fields

    @ManyToMany(targetEntity=Group.class)
    private List<Group> groups;
}

In my interface, I'm trying to provide a way to add users to a group in a sort of bulk-entry form. Is there a way to allow the insertion of a new many-to-many entry, without having to load the entire collection of users in a group first?

One of the side has to be the inverse side, using mappedBy . You just need to update the owner side.

So if you make User the owner side, you just need to add the group to the user.

If that is not sufficient, then consider using SQL.

Also, targetEntity is redundant with the generic type of the list. You don't need it.

No, there is no way to do that with JPQL, although you did not define "in a sort of bulk-entry form". Basically you either use Native Queries (ie SQL queries) or fetch the whole collections. See this question for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM