spring-data-jpa:ORA-01795:列表中的最大表达式数为1000

时间:2016-11-07 09:47:38

标签: java oracle hibernate spring-data-jpa

我正在使用Spring Data JPA。我想从List<String> clientIdList获取client.id的事务。问题是我传递了一个非常大的列表,我收到了一个ORA-01795错误。

@Query(value = "SELECT TransactRepViewModel FROM TransactRepViewModel a WHERE a.clientId IN (?1) AND a.clDate BETWEEN ?2 and ?3", nativeQuery = true)
    List<TransactRepViewModel> findByClientIdList(List<String> clientIdList, Date startDate, Date endDate) throws DataAccessException;

我的客户端列表来自另一个通过oracle的数据库的另一个表,我想不出解决这个问题的方法......

编辑:列表是动态的,因此它可以返回不同数量的id。我也无法在这些数据库中创建任何其他表。我没有这样的特权。

2 个答案:

答案 0 :(得分:6)

您可以将clientID列表分成999个元素列表,并对DB进行多次调用。您可以使用Apache Commons ListUtils to do the partitioning

  List<TransactRepViewModel> result = new ArrayList<TransactRepViewModel>();
  final List<List<String>> partitions = ListUtils.partition(clientIdList, 999);
  for (List<String> partition : partitions) {
     result.addAll(yourRepo.findByClientIdList(partition, startDate, endDate);)
  }

答案 1 :(得分:1)

您可以分两步拆分此操作:

  1. 在表格中插入您的ID列表(例如临时表格)
  2. 将包含大量值的in语句更改为in语句,并在此新(临时)表上使用subselect。