简体   繁体   中英

JPA Criteria Specification for ManyToMany

I had entities Client and Agency with ManyToOne relation, so that table client had column agency_id pointing to table agency , also Client had:

@ManyToOne
@JoinColumn(name = "agency_id")
private Agency agency;

There were Spring Data JPA specification to select Client 's with Agency id's in:

public static Specification<Client> withAgencyIds(Collection<Long> agencyIds) {
    return (root, query, cb) -> root.join(Client_.agency).get(Agency_.id).in(agencyIds);
}

Now I have to change ManyToOne relation to ManyToMany relation, so that Client have:

@ManyToMany
@JoinTable(name = "client_agency",
           joinColumns = @JoinColumn(name = "client_id"),
           inverseJoinColumns = @JoinColumn(name = "agency_id"))
@OrderBy("name")
private List<Agency> agencies = new ArrayList<>();

How should I tune the specification above in order to select Client 's having Agency id's?

Thank you very much in advance!

This JPA Criteria specification works fine:

public static Specification<Client> withAgencyIds(Collection<Long> agencyIds) {
    return (root, query, cb) -> root.join(Client_.agencies).get(Agency_.id).in(agencyIds);
}

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