Java Spring JPA属性表达式通过列表中项目的交集查找

Java Spring JPA属性表达式通过列表中项目的交集查找,java,spring,spring-data,spring-data-jpa,Java,Spring,Spring Data,Spring Data Jpa,如何使用属性表达式编写JPA存储库方法,以检查列表中是否存在多个项或这些项上的属性?我可以在列表中查找单个项目,请参见下面的邮政编码,但我正在尝试编写一种方法来检查多个邮政编码,结果集中的每个人在他们的地址列表中都有两个邮政编码 @Repository public interface PersonRepository extends CrudRepository<Person, Long> { // Works Set<Person> findAllBy

如何使用属性表达式编写JPA存储库方法,以检查列表中是否存在多个项或这些项上的属性?我可以在列表中查找单个项目,请参见下面的邮政编码,但我正在尝试编写一种方法来检查多个邮政编码,结果集中的每个人在他们的地址列表中都有两个邮政编码

@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
    // Works
    Set<Person> findAllByAddresses_ZipCode(String zip);

    // Doesn't work - does not return expected results
    Set<Person> findAllByAddresses_ZipCode_And_Addresses_ZipCode(String zip1, String zip2);
}
@存储库
公共接口PersonRepository扩展了Crudepository{
//工作
设置findallbyaddress_ZipCode(字符串zip);
//不工作-不返回预期结果
设置findallbyaddress_ZipCode_和_Addresses_ZipCode(字符串zip1,字符串zip2);
}
我目前的做法是用2个邮政编码取两组,然后找到两组的交集:

public @ResponseBody
    Iterable<Person> personsWithBothZipCodes(@PathVariable("zip1") String zip1,
                                             @PathVariable("zip2") String zip2) {

    Set<Person> a = personRepository.findAllByAddresses_ZipCode(zip1);
    Set<Person> b = personRepository.findAllByAddresses_ZipCode(zip2);

    // Only return results that contain both zip1 and zip2.
    a.retainAll(b);

    return a;
}
public@ResponseBody
两个ZipCodes(@PathVariable(“zip1”)字符串zip1,
@路径变量(“zip2”)字符串(zip2){
Set a=personRepository.findallbyaddress_ZipCode(zip1);
Set b=personRepository.findallbyaddress_ZipCode(zip2);
//仅返回同时包含zip1和zip2的结果。
a、 保留(b);
返回a;
}
实体如下所示:

@Entity
public class Person
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    // zipcode is a String property on an Address.
    @OneToMany(targetEntity = com.data.Address.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Address> addresses = new ArrayList<Address>();
    ...
}
@实体
公共阶层人士
{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
//zipcode是地址上的字符串属性。
@OneToMany(targetEntity=com.data.Address.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)

私有列表

是,只需在
中添加单词

Set<Person> findAllByAddresses_ZipCodeIn(Set<String> zip);
Set-findallbyaddress\u-ZipCodeIn(Set-zip);
然后在控制器中,您可以执行以下操作:

public @ResponseBody Iterable<Person> personsWithBothZipCodes(@PathVariable("zip1") String zip1, @PathVariable("zip2") String zip2) {

    Set<String> zipSet = new HashSet<>();
    zipSet.add(zip1);
    zipSet.add(zip2);

    Set<Person> a = personRepository.findAllByAddresses_ZipCodeIn(zipSet);

    return a;
}
public@ResponseBody Iterable persons,两个zipCodes(@PathVariable(“zip1”)字符串zip1,@PathVariable(“zip2”)字符串zip2){
Set zipSet=new HashSet();
zipSet.add(zip1);
zipSet.add(zip2);
Set a=personRepository.findallbyaddress_ZipCodeIn(zipSet);
返回a;
}
不知道这是否有效,但可以尝试一下

Set<Person> findAllByAddresses_ZipCodeInAndZipCodeIn(Set<String> zip1, Set<String> zip2);

public @ResponseBody Iterable<Person> personsWithBothZipCodes(@PathVariable("zip1") String zip1, @PathVariable("zip2") String zip2) {

    Set<String> zipSet1 = new HashSet<>();
    zipSet1.add(zip1);

    Set<String> zipSet2 = new HashSet<>();
    zipSet2.add(zip2);

    Set<Person> a = personRepository.findAllByAddresses_ZipCodeInAndZipCodeIn(zipSet1, zipSet2);

    return a;
}
Set findallbyaddress\u ZipCodeInAndZipCodeIn(Set zip1,Set zip2);
public@ResponseBody Iterable persons,具有两个zipCodes(@PathVariable(“zip1”)字符串zip1,@PathVariable(“zip2”)字符串zip2){
Set zipSet1=new HashSet();
zipSet1.add(zip1);
Set zipSet2=新的HashSet();
zipSet2.add(zip2);
Set a=personRepository.findallbyaddress_ZipCodeInAndZipCodeIn(zipSet1,zipSet2);
返回a;
}

是,只需将
中的
一词添加到查询中即可

Set<Person> findAllByAddresses_ZipCodeIn(Set<String> zip);
Set-findallbyaddress\u-ZipCodeIn(Set-zip);
然后在控制器中,您可以执行以下操作:

public @ResponseBody Iterable<Person> personsWithBothZipCodes(@PathVariable("zip1") String zip1, @PathVariable("zip2") String zip2) {

    Set<String> zipSet = new HashSet<>();
    zipSet.add(zip1);
    zipSet.add(zip2);

    Set<Person> a = personRepository.findAllByAddresses_ZipCodeIn(zipSet);

    return a;
}
public@ResponseBody Iterable persons,两个zipCodes(@PathVariable(“zip1”)字符串zip1,@PathVariable(“zip2”)字符串zip2){
Set zipSet=new HashSet();
zipSet.add(zip1);
zipSet.add(zip2);
Set a=personRepository.findallbyaddress_ZipCodeIn(zipSet);
返回a;
}
不知道这是否有效,但可以尝试一下

Set<Person> findAllByAddresses_ZipCodeInAndZipCodeIn(Set<String> zip1, Set<String> zip2);

public @ResponseBody Iterable<Person> personsWithBothZipCodes(@PathVariable("zip1") String zip1, @PathVariable("zip2") String zip2) {

    Set<String> zipSet1 = new HashSet<>();
    zipSet1.add(zip1);

    Set<String> zipSet2 = new HashSet<>();
    zipSet2.add(zip2);

    Set<Person> a = personRepository.findAllByAddresses_ZipCodeInAndZipCodeIn(zipSet1, zipSet2);

    return a;
}
Set findallbyaddress\u ZipCodeInAndZipCodeIn(Set zip1,Set zip2);
public@ResponseBody Iterable persons,具有两个zipCodes(@PathVariable(“zip1”)字符串zip1,@PathVariable(“zip2”)字符串zip2){
Set zipSet1=new HashSet();
zipSet1.add(zip1);
Set zipSet2=新的HashSet();
zipSet2.add(zip2);
Set a=personRepository.findallbyaddress_ZipCodeInAndZipCodeIn(zipSet1,zipSet2);
返回a;
}

也许您可以使用JPQL查询(如注释中所建议的)

@Query(“以个人身份从个人”+
“将person.addresses作为address1与address1.zipCode=?1联接”+
“将person.addresses作为address2与address2.zipCode=?2”连接起来
设置findbyzipcode(字符串zipCode1,字符串zipCode2);

还没有真正复制您的案例,但它可能会起作用。

也许您可以使用JPQL查询(如评论中所建议的)

@Query(“以个人身份从个人”+
“将person.addresses作为address1与address1.zipCode=?1联接”+
“将person.addresses作为address2与address2.zipCode=?2”连接起来
设置findbyzipcode(字符串zipCode1,字符串zipCode2);

尚未真正复制您的案例,但它可能会起作用。

这并不能完全解决我的问题。我想获取具有所有给定拉链的人员,而不仅仅是列表中的一个,因此我的交集设置在控制器中。澄清了问题。请参阅我的编辑。我不知道它是否起作用,但可以尝试添加另一个拉链,因此它将是
InadIn
findAllByAddresses\u ZipCodeInadIn
不太管用,我认为最接近这一点的是
findAllByAddresses\u ZipCodeInAdzipCodeIn
,但它断言两个输入参数中都有相同的地址邮政编码。这不是我需要的解决方案。我想我可能必须坚持我最初的破解方法,但谢谢分享你的想法我不知道“in”关键字。可能需要将
Person
连接到
Address
两次。这样,结果集将返回类似Person、address1.zip、address2.zip的元组。这可能很难用spring magic解决。@Gaʀ。查看我的编辑。这并不能完全解决我的问题。我想获取具有所有给定ZIP的人员,而不仅仅是列表中的一个,因此我的交叉点设置在控制器中。澄清了问题。查看我的编辑。我不知道它是否有效,但可以尝试在另一个中添加,因此它将是
InAdIn
findAllByAddresses\u ZipCodeInAdIn
不太管用,我想最接近的是
findallbyaddress\u ZipCodeInAndZipCodeIn
,但它在两个输入参数中都声明了相同的地址邮政编码。这不是我需要的解决方案。我想我可能必须坚持我最初的破解,但感谢你分享你的想法。我不知道“in”关键字。可能需要将
Person
连接到
Address
两次。这样,结果集将返回元组