我正在使用Spring工具套件在spring MVC项目和spring数据中工作,我希望将日期参数传递给本机查询,到目前为止我已经这样做了。
我在扩展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
答案 0 :(得分:9)
你可以使用这样的结构:
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);