Spring Data JPA How to pass date parameter

I am working on a spring MVC and spring project with spring toolkit, I want to pass date argument to my own query, I have it for now.

My request method inside an interface that extends 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);


The method that implements this interface definition:

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

            ArrayList<Object> results= null;

            results= serviceAutowiredVariable.personInfo(name, dateBirthDay);

            return results;


and this is how I call it from my controller class.

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


I believe this line "AND a.birthday = ?2 "


is equal to this lineTO_DATE('01-08-2013', 'DD-MM-YYYY')

but i get this error when i run my code

    [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



source to share

1 answer

You can use a construction like this:

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);




All Articles