I am working on Eclipselink named query and have issues in setting optional parameter for timestamp.

My requirement is to query data with timestamp if the field is entered by the user. As the field is optional, I am checking for ''0000-00-00 00:00:00' or the user input date. The query snippet is as follows. However it doesn't work.

DataConstants.java
==================
public static String DATE_CHECKING_FORMAT = "0000-00-00 00:00:00"

Named Query
===========
entityManager.createNamedQuery("Requests.SearchAllData")
.setParameter("fromTime", ((selectionCriteria.getFromRequestTime() == null)? DataConstants.DATE_CHECKING_FORMAT : selectionCriteria.getFromRequestTime()))
.setFirstResult(offset).setMaxResults(limit).getResultList();

@NamedQuery(name = "Requests.SearchAllData", 
   query = "select ws.requestTimeStamp from StudentBE ws where ((:fromTime = to_timestamp('0000-00-00 00:00:00','dd-mm-yyyy hh24:mi:ss')) or (ws.requestTimeStamp >= :fromTime))), 

While executing, I am getting this error.

Exception Description: Syntax error parsing the query [Requests.SearchAllData: select ws.requestTimeStamp from StudentBE ws where ((:fromTime = to_timestamp('0000-00-00 00:00:00','dd-mm-yyyy hh24:mi:ss')) or (ws.requestTimeStamp >= :fromTime)), line 1, column 63: syntax error at [=]. Internal Exception: MismatchedTokenException(82!=84)

Please suggest how to use optional timestamp field.

Thanks!!!

有帮助吗?

解决方案

to_timestamp() is a database function, but you are using JPQL for your query.

For the JPQL timestamp format see,

http://en.wikibooks.org/wiki/Java_Persistence/JPQL#Literals

If you really want to use a database function, you can use the FUNC or FUNCTION operator.

You may also want to declare the constant as a parameter instead, then just set it as a parameter when you execute the query.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top