Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JPA repository在列表中查找-允许空_Spring_Hibernate_Jpa_Spring Data_Findby - Fatal编程技术网

Spring JPA repository在列表中查找-允许空

Spring JPA repository在列表中查找-允许空,spring,hibernate,jpa,spring-data,findby,Spring,Hibernate,Jpa,Spring Data,Findby,简短描述 当数组列表输入为空时,如何使findByIn与IN一起工作。e、 忽略它。你的刀是什么样子的 更详细的描述。 假设您创建了一个搜索用户页面 在应用程序中。您可以使用各种选项进行筛选 已创建(始终给定日期范围) 国家(空时忽略并搜索所有国家) 阿格朗格 职称 等等 现在假设您要在国家列表中搜索给定日期范围内的所有用户 搜索用户时,我将始终搜索加入日期,但如果我没有选择国家,我希望它搜索所有国家 我计划增加几个过滤选项,而不是国家/地区。所以我并不想为每个可能的字段组合创建很多find

简短描述

当数组列表输入为空时,如何使
findByIn
IN
一起工作。e、 忽略它。你的刀是什么样子的

更详细的描述。

假设您创建了一个搜索用户页面

在应用程序中。您可以使用各种选项进行筛选

  • 已创建(始终给定日期范围)
  • 国家(空时忽略并搜索所有国家)
  • 阿格朗格
  • 职称
  • 等等
现在假设您要在国家列表中搜索给定日期范围内的所有用户

搜索用户时,我将始终搜索加入日期,但如果我没有选择国家,我希望它搜索所有国家

我计划增加几个过滤选项,而不是国家/地区。所以我并不想为每个可能的字段组合创建很多findBy方法

DAO

@Repository
public interface UserDao extends JpaRepository<User, Long> {

    public List<BeatRate> findByCreatedBetweenAndCountryIn(Date from, Date to, ArrayList<String> countryList );

}
@存储库
公共接口UserDao扩展了JpaRepository{
在和CountryIn之间创建的公共列表(日期从,日期到,ArrayList countryList);
}
测试

@Test
public void test() throws ParseException {

    Date from = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2015-01-01" );
    Date to   = new SimpleDateFormat("yyyy-MM-dd").parse("2015-05-15");

    //ArrayList<String> countryList = new ArrayList<String>();
    //countryList.add("UK");
    //countryList.add("Australia");
    //countryList.add("Japan");   // works ok when I have a list

    countryList = null;  // I want it to search for all countries when this is null -- this errors and doesnt work..  

    List<BeatRate> beatRates = beatRateDao.findByCreatedBetweenAndRentalCountryIn(from, to, countryList);

    Assert.assertTrue(beatRates.size()>0);

}
@测试
public void test()引发异常{
日期自=新的简化格式(“yyyy-MM-dd”)。解析(“2015-01-01”);
日期至=新的简化格式(“yyyy-MM-dd”)。解析(“2015-05-15”);
//ArrayList countryList=新的ArrayList();
//国家列表。添加(“英国”);
//国家名单。添加(“澳大利亚”);
//countryList.add(“Japan”);//当我有一个列表时可以正常工作
countryList=null;//我希望它在该值为null时搜索所有国家——这会出错并且不起作用。。
List beatRates=beatRateDao.FindByCreatedBetween and EntalCountryIn(from,to,countryList);
Assert.assertTrue(beatRates.size()>0);
}

您可以有两种方法:

beatRateDao.findByCreatedBetweenAndRentalCountryIn(from, to, countryList);

然后根据
国家列表选择一个:

List<BeatRate> beatRates = (countryList != null && !countryList.isEmpty())
    ?  beatRateDao.findByCreatedBetweenAndRentalCountryIn(from, to, countryList)
    : beatRateDao.findByCreatedBetweenAndRental(from, to);
您将得到以下错误:

ERROR:  syntax error at or near ")"
LINE 3: where quantity in ( )
                            ^
********** Error **********

ERROR: syntax error at or near ")"
SQL state: 42601
Character: 45

谢谢你的答复。。问题是我有几个过滤器。状态、国家、城市、用户。。。有时国家存在有时不存在有时你想要地位和国家而不是城市。。等它会导致许多组合,因此我不确定上述方法是否是最佳方法?Criteria API最适用于动态过滤器。Spring数据不能使用空参数。什么是标准API?是一个JPA类型安全的动态查询生成器API。如果您想基于任意约束构造查询,我建议您进行研究。这是迄今为止最适合动态组装谓词的API。Spring数据派生的查询方法更适合于具有静态约束集的用例。请参阅关于Spring与Querydsl的数据集成。
select * 
from product 
where quantity in ( )
ERROR:  syntax error at or near ")"
LINE 3: where quantity in ( )
                            ^
********** Error **********

ERROR: syntax error at or near ")"
SQL state: 42601
Character: 45