I want to execute this query in JPA.But i am getting error while executing this query.How to use Month(),Year() in JPA We can use these in native SQL Query.But how to use i
Few JPA implementations have built-in support for date/time functions.
EclipseLink
EclipseLink supports EXTRACT allowing any database supported date/time part value to be extracted from the date/time. The EXTRACT function is database independent, but requires database support.
EXTRACT(YEAR, model.currencyExchangeDate)
, but it requires EclipseLink 2.4.x
FUNC allows for a database function to be call from JPQL. It allows calling any database functions not supported directly in JPQL.
To call the DB function MyFunc(a, b, c)
use FUNC('MyFunc', a, b, c)
.
You can try FUNC('YEAR', currencyExchangeDate)
to call 'YEAR' function.
Hibernate
In HQL, you can have date function YEAR(date)
, MONTH(date)
, DAY(date)
to extract required details.
Other time functions supported are HOUR(date)
, MINUTE(date)
, SECOND(date)
.
Criteria API
CriteriaBuilder#function() : Create an expression for the execution of a database function.
CriteriaQuery
exRateLine.get(exRateLine_.currencyExchangeDate)), year)
.and(cb.equal(cb.function("month", Integer.class,
exRateLine.get(exRateLine_.currencyExchangeDate)), month)));