2016-08-19 65 views
0

我正在使用Spring的JPA使用namedquery來獲取其他api。命名查詢是在我的實體類實現:IllegalArgumentException:使用Spring的NamedQuery JPA

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

當我開始春季啓動應用程序並輸入網址「http://localhost:8080/ristore/foundation/SKM1」,我得到了以下錯誤:

java.lang.IllegalArgumentException: Parameter with that position [1] did not exist 

我做了什麼錯?

回答

2

看起來你不能使用基於我閱讀的文檔@NamedQuery命名參數。您是否用?1來嘗試?

命名參數不起作用的原因是您還必須在方法參數上添加註釋,以便Spring知道哪個參數與查詢中的佔位符匹配。

+0

它現在正常工作,如果我想使命名參數工作,我應該在哪裏「在方法參數上添加註釋」? – ddd

+0

我沒有看到您的存儲庫接口/類,但這是它將完成的地方。查看'@ Query'周圍的spring-data-jpa文檔 –