2015-09-07 107 views
1

我試圖調用MS SQL Server存儲過程。我使用彈簧引導,JPA 2.1,休眠。調用SQL Server存儲過程使用JPA 2.1註解

數據庫有ISBN,書名,作者,描述和存儲過程中我試圖調用表需要一個參數(ISBN)作爲一個字符串,並返回只有標題。

我得到以下錯誤:

org.hibernate.procedure.ParameterStrategyException: 
Attempt to access positional parameter [2] but ProcedureCall using named parameters 

任何人都得到一個解決方案,這或知道有什麼錯誤意味着什麼?我也嘗試了其他註釋組合。

Book.java

@Entity 
@NamedStoredProcedureQuery(
     name = "bookList", 
     resultClasses=Book.class, 
     procedureName = "dbo.list_books", 
     parameters = { 
      @StoredProcedureParameter(mode = ParameterMode.IN, name = "isbn", type = String.class) 
      }) 
public class Book { 

    @Id 
    private String title; 

    public String getTitle() { 
     return title; 
    } 
    public void setTitle(String title) { 
     this.title = title; 
    }  
} 

BookRepository.java

@Repository 
public interface BookRepository extends CrudRepository<Book, Long> { 

    @Procedure 
    Iterable<Book> list_books(String arg); 
} 

BookService.java

@RestController 
@RequestMapping(value = "/books", produces = MediaType.APPLICATION_JSON_VALUE) 
public class BookService { 

    @Autowired 
    protected BookRepository bookRepository; 

    @RequestMapping 
    public Iterable<Book> books(){ 
     return bookRepository.getBooks("1111111"); 
    } 

回答

0

我沒有註釋解決這個問題,我的工作圍繞它與EntityManager和StoredProcedureQuery。

的Book.java是一樣的,但沒有@NamedStoredProcedureQuery。我刪除了存儲庫,並改寫了這樣的服務:

@RestController 
@RequestMapping("/api") 
public class BookService { 

    @RequestMapping(value = "/books", 
      params = {"isbn"}, 
      method = RequestMethod.GET, 
      produces = MediaType.APPLICATION_JSON_VALUE) 
    public List<Book> getByIsbn(@RequestParam(value = "isbn") String isbn){ 
     StoredProcedureQuery sp = em.createStoredProcedureQuery("name.of.stored.procedure", Book.class);    
     sp.registerStoredProcedureParameter("isbn", String.class, ParameterMode.IN); 
     sp.setParameter("isbn", isbn); 

     boolean result = sp.execute(); 
     if (result == true) { 
      return sp.getResultList(); 
     } else { 
      // Handle the false for no result set returned, e.g. 
      throw new RuntimeException("No result set(s) returned from the stored procedure"); 
     } 
} 

} 

現在可以調用此端點像stringquery:http://localhost/api/books?isbn=1111111

相關問題