2014-09-24 95 views
3

我正在使用spring工具套件處理Spring MVC項目和spring數據,我想將日期參數傳遞給本機查詢,至此爲止我已經完成。Spring Data JPA如何傳遞日期參數

延伸JpaRepository

@Query(value = 
      "SELECT " 
       + "a.name, a.lastname 
      + "FROM " 
       + "person a, " 
       + "myTable b " 
      + "WHERE " 
      + "a.name= ?1' " 
      + "AND a.birthday = ?2 ", 
     nativeQuery = true) 
    public ArrayList<Object> personInfo(String name, String dateBirthDay); 

實現該接口定義的方法的界面內我的查詢方法:

public ArrayList<Object> getPersonsList(String name, String dateBirthDay) { 

      ArrayList<Object> results= null; 

      results= serviceAutowiredVariable.personInfo(name, dateBirthDay); 

      return results; 
     } 

,這是我如何把它從我的控制器類。

personsList= _serviceAutowiredVariable.getPersonsList("Jack", "TO_DATE('01-08-2013', 'DD-MM-YYYY')"); 

我想,在這條線"AND a.birthday = ?2 "?2是等於這個字符串TO_DATE('01-08-2013', 'DD-MM-YYYY')

但是當我跑我得到這個錯誤我的代碼

[Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.DataException: could not extract ResultSet; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not extract ResultSet] root cause 
java.sql.SQLDataException: ORA-01858: a non-numeric character was found where a numeric was expected 

ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ORA-01858: a non-numeric character was found where a numeric was expected 
+1

只需傳遞一個'Date'對象而不是'St ring'。 – 2014-09-24 18:41:21

回答

6

您可以使用這樣的結構:

import org.springframework.data.repository.query.Param; 
... 

@Query(value = 
    " SELECT a.id, a.lastname FROM person a" + 
    " WHERE a.name = :name AND a.birthday = :date ", nativeQuery = true) 
public List<Object[]> getPersonInfo(
    @Param("name") String name, 
    @Param("date") Date date); 
+0

我犯了錯字 - getPersonInfo需要2個參數:類型爲String和Date。我剛剛編輯來解決這個問題。 – 2014-09-24 19:55:55

+0

感謝它的工作 – stackUser2000 2014-09-24 20:43:54

相關問題