Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare timestamp with date without time in JPQL?

Tags:

jpa

jboss

jpql

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?

like image 952
Lukasz Puchala Avatar asked Dec 02 '14 14:12

Lukasz Puchala


People also ask

Is JPQL simpler than SQL?

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.

Can we use inner join in JPQL?

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

Which methods should be used for pagination with JPQL?

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.


2 Answers

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() 
like image 107
Arun Nair Avatar answered Sep 20 '22 14:09

Arun Nair


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)
like image 37
Master Slave Avatar answered Sep 19 '22 14:09

Master Slave