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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30511372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:44:39  来源:igfitidea点击:

Parameter with that position [5] did not exist;

javamysqlspring

提问by Stan

I'm trying to request some data in my database via a query, but all I get is this exception:

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

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

Well and this is my my MappingController

嗯,这是我的 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";
}

I think I don't need to write down the ServiceClass but below is the ServiceClassImplementation

我想我不需要写下 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);

}

Last but not least this is my repository:

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

@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);

采纳答案by Predrag Maric

Position based parameters start with 1, try with this

基于位置的参数从 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);

or, with unchanged method signature

或者,方法签名不变

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

回答by Stan

From this reference(emphasis is mine):

从这个参考(重点是我的):

Parameters to SQL queries are delimited using the ? character. Only indexed parameters are supported, named parameters are not supported. The index can be used in the delimiter, i.e. ?1. Parameter values are set on the Query using the setParameter API. Indexed parameters start at the index 1 not 0.

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

So you have to change your query to:

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

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

see also, this tutorial from Oracle

另请参阅Oracle 的教程