stackUser2000 stackUser2000 - 5 months ago 5x
Java Question

Spring Data JPA How to pass a date parameter

I'm working in a spring MVC project and spring data with spring tools suite, I want to pass a date argument to a native query, I have this so far.

My query method inside a interface that extends JpaRepository

@Query(value =
+ ", a.lastname
+ "FROM "
+ "person a, "
+ "myTable b "
+ "WHERE "
+ " ?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 suppose that in this line
"AND a.birthday = ?2 "
is equals to this string
TO_DATE('01-08-2013', 'DD-MM-YYYY')

but I am getting 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


You can use such construction:


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