简体   繁体   English

众多的JPA标准规范

[英]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关系的实体ClientAgency ,因此表client agency_id列指向表agency ,并且Client具有:

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

There were Spring Data JPA specification to select Client 's with Agency id's in: 有Spring Data JPA规范,可以在以下位置选择具有Agency ID的Client

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: 现在,我必须将ManyToOne关系更改为ManyToMany关系,以便Client具有:

@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? 我应该如何调整上面的说明书中,以选择Client的有Agency的ID?

Thank you very much in advance! 提前非常感谢您!

This JPA Criteria specification works fine: 此JPA标准规范可以正常工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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