Java Spring数据JPA如何传递日期参数

Java Spring数据JPA如何传递日期参数,java,spring,spring-mvc,jpa,spring-data,Java,Spring,Spring Mvc,Jpa,Spring Data,我在一个SpringMVC项目和SpringDatawithSpringToolsSuite中工作,我想把一个日期参数传递给一个本机查询,到目前为止我已经有了这个 我的查询方法在扩展JpaRepository的接口中 @Query(value = "SELECT " + "a.name, a.lastname + "FROM " + "person a, "

我在一个SpringMVC项目和SpringDatawithSpringToolsSuite中工作,我想把一个日期参数传递给一个本机查询,到目前为止我已经有了这个

我的查询方法在扩展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);
我假设在这行
“和a.birth=?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

您可以使用以下构造:

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);
import org.springframework.data.repository.query.Param;
...
@查询(值=
“从人员a中选择a.id、a.lastname”+
“其中a.name=:name和a.birth=:date”,nativeQuery=true)
公共列表getPersonInfo(
@参数(“名称”)字符串名称,
@参数(“日期”)日期;

只需传递一个
日期
对象,而不是
字符串
。我犯了打字错误-getPersonInfo接受两个参数:当然是字符串类型和日期类型。我刚刚编辑以修复此问题。
    [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
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);