我正在使用命名查询来使用Spring JPA进行rest api。命名查询在我的实体类中实现:
@Entity
@Table(name="SPECIMEN_TB")
@NamedQueries({
@NamedQuery(name="SpecimenTb.findBySpecimenNo", query="select s from SpecimenTb s where s.specimenNo = :specimenNo"),
})
public class SpecimenTb implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SPECIMEN_TB_ROWID_GENERATOR")
@Column(name="ROW_ID")
private long rowId;
@Column(name="SPECIMEN_NO", unique = true)
private String specimenNo;
我的控制器看起来像这样:
@RestController
public class RistoreController {
@Autowired
private RistoreService ristoreService;
@RequestMapping(
value = "/ristore/foundation/{specno}",
method = RequestMethod.GET,
produces = "application/json")
public ResponseEntity<SpecimenTb> getFmSpecimen(@PathVariable("specno") String specno) {
List<SpecimenTb> specimens = ristoreService.findBySpecimenNo(specno);
if (specimens == null) {
return new ResponseEntity<SpecimenTb>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<SpecimenTb>(specimens.get(0), HttpStatus.OK);
}
我有一个服务bean,它调用JPA存储库findBySpecimenNo方法。
@Service
public class RistoreServiceBean implements RistoreService {
@Autowired
private SpecimenRepository specimenRepository;
@Override
public List<SpecimenTb> findAll() {
List<SpecimenTb> specimens = specimenRepository.findAll();
return specimens;
}
@Override
public List<SpecimenTb> findBySpecimenNo(String specimenNo) {
List<SpecimenTb> specimens = specimenRepository.findBySpecimenNo(specimenNo);
return specimens;
}
当我启动Spring Boot应用程序并输入网址“http://localhost:8080/ristore/foundation/SKM1”时,我收到以下错误:
java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
我做错了什么?
答案 0 :(得分:2)
根据我阅读的文档,您似乎无法使用@NamedQuery
的命名参数。您是否尝试使用?1
?
命名参数不起作用的原因是您还必须在方法参数上添加注释,以便Spring知道哪个参数与查询中的占位符匹配。