IllegalArgumentException:使用 Spring JPA 的 NamedQuery
IllegalArgumentException: NamedQuery using Spring JPA
我正在使用 namedquery 作为休息 api 使用 Spring JPA。命名查询在我的实体 class:
中实现
@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);
}
我有一个调用 JPA 存储库 findBySpecimenNo 方法的服务 bean。
@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 引导应用程序并输入 url“http://localhost:8080/ristore/foundation/SKM1”时,我收到以下错误:
java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
我做错了什么?
根据我阅读的文档,您似乎不能将命名参数与 @NamedQuery
一起使用。您是否尝试过使用 ?1
?
命名参数不起作用的原因是您还必须在方法参数上添加注释,以便Spring知道哪个参数与查询中的占位符匹配。
我正在使用 namedquery 作为休息 api 使用 Spring JPA。命名查询在我的实体 class:
中实现@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);
}
我有一个调用 JPA 存储库 findBySpecimenNo 方法的服务 bean。
@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 引导应用程序并输入 url“http://localhost:8080/ristore/foundation/SKM1”时,我收到以下错误:
java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
我做错了什么?
根据我阅读的文档,您似乎不能将命名参数与 @NamedQuery
一起使用。您是否尝试过使用 ?1
?
命名参数不起作用的原因是您还必须在方法参数上添加注释,以便Spring知道哪个参数与查询中的占位符匹配。