具有该位置的参数[5]不存在;

时间:2015-05-28 15:48:22

标签: java mysql spring

我试图通过查询在我的数据库中请求一些数据,但我得到的只是这个例外:

HTTP Status 500 - Request processing failed; nested exception is
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [5] did
not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [5]
did not exist

好吧,这是我的MappingController

@RequestMapping(value="/vacChange", method = RequestMethod.POST)
public String changedVac(@RequestParam(value = "id", required = true) Integer id,
                         @RequestParam(value = "ort", required = true) String ort,
                         @RequestParam(value = "bereich", required = true) String bereich,
                         @RequestParam(value = "beschreibung", required = true) String beschreibung){
vacService.changeVacancyByID(id,gehalt,ort,bereich,beschreibung);


    return "vacAdmin";
}

我认为我不需要写下ServiceClass,但下面是ServiceClassImplementation

public void changeVacancyByID(Integer id, String gehalt,String ort,String bereich,String beschreibung){
        System.out.println("Edit method called");
        VacancyEntity vacEntity = vacancyRepository.findOneById(id);
        vacancyRepository.updateAttributes(id,gehalt,ort,bereich,beschreibung);

}

最后但并非最不重要的是这是我的存储库:

@Transactional
@Query (value = "UPDATE vacancy SET salary=?1, location=?2,functionality=?3, description=?4 WHERE id = ?0  ", nativeQuery = true)
VacancyEntity updateAttributes(Integer id, String gehalt, String ort, String bereich, String beschreibung);

2 个答案:

答案 0 :(得分:15)

基于位置的参数从1开始,尝试使用此

@Query (value = "UPDATE vacancy SET salary=?1, location=?2,functionality=?3, description=?4 WHERE id = ?5  ", nativeQuery = true)
VacancyEntity updateAttributes(String gehalt, String ort, String bereich, String beschreibung, Integer id);

或者,方法签名未更改

@Query (value = "UPDATE vacancy SET salary=?2, location=?3,functionality=?4, description=?5 WHERE id = ?1  ", nativeQuery = true)

答案 1 :(得分:2)

reference开始(重点是我的):

  

SQL查询的参数是使用?分隔的?字符。仅支持索引参数,不支持命名参数。索引可以用在分隔符中,即?1。使用setParameter API在Query上设置参数值。 索引参数从索引1开始而不是0。

因此您必须将查询更改为:

@Query(value = "UPDATE vacancy SET salary=?2, location=?3,functionality=?4, description=?5 WHERE id = ?1", nativeQuery = true)

另请参阅此tutorial from Oracle