I'd like to get objects with create_date = :my_date
where create_date
is in 2014-12-02 14:49:15.029
format and my_date
is in 2014-12-02
format in JPA.
Is there any way to compare timestamp column with Date object (yyyy-MM-dd format) in JPQL using NamedQuery
or should I use native query?
JPQL syntax is very similar to the syntax of SQL. Having SQL like syntax is an advantage because SQL is a simple structured query language and many developers are using it in applications. SQL works directly against relational database tables, records and fields, whereas JPQL works with Java classes and instances.
JPQL provides an additional type of identification variable, a join variable, which represent a more limited iteration over specified collections of objects. In JPQL, JOIN can only appear in a FROM clause. The INNER keyword is optional (i.e. INNER JOIN is equivalent to JOIN).
Solution: With JPA and Hibernate, you have to set the pagination information on the Query interface and not in the query String as you know it from SQL. You can do that by calling the setFirstResult(int startPosition) and setMaxResults(int maxResults) methods.
If you are using jpql try to use
date(timestamp_column)
eg :
@Query(select te from Testentity te where date(te.createdTimestamp) >= CURRENT_DATE)
public List<Testentity> getTestDate()
You can use NamedQuery, you need to properly manipulate the @Temporal annotation and the TemporalType enum.
So if your object property is a Date without a time portion, you would annotate it as @Temporal(TemporalType.DATE)
, further on when supplying a parameter to your named query you would use one of the two available signatures
setParameter(String name, java.util.Date value, TemporalType temporalType)
setParameter(String name, java.util.Calendar value, TemporalType temporalType)
so your query will have a part like:
query.setParameter("my_date", myDate, TemporalType.DATE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With