使用的代码为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Component public interface PhotoRepository extends CrudRepository<Photo, Long> { @Query("select count(cosId) from Photo p where p.cosId = ?2") int fixPartialCheckCount(Integer price, String cosId); }
@RequestMapping(value = "photo/data-import-fix-partial-check", method = RequestMethod.POST) public @ResponseBody Integer fixPartialCheckCount(@RequestBody Photo req) { int count = 0; try { count = photoRepository.fixPartialCheckCount(req.getPrice(), req.getCosId()); } catch (Exception e) { System.out.println("duplicate " + e.getMessage()); } return count; }
|
得到了题目上的这个异常。尝试将fixPartialCheckCount
改为
1 2 3 4 5 6 7
| @Query("select count(cosId) from Photo p where p.cosId = ?1") int fixPartialCheckCount(String cosId);
count = photoRepository.fixPartialCheckCount(req.getCosId());
|
则正常运行并符合预期。
JPA要求输入参数列表必须全都使用吗?
继续尝试下面的更改:
1 2 3 4 5 6 7
| @Query("select count(cosId) from Photo p where p.cosId = ?1") int fixPartialCheckCount(String cosId, Integer price);
count = photoRepository.fixPartialCheckCount(req.getCosId(), req.getPrice());
|
也正常运行。
所以猜测,@Query
必须要保证至少?1
要有。