Subsitute for Month,year,date functions in jpa

前端 未结 3 1925
Happy的楠姐
Happy的楠姐 2021-01-14 15:41

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

3条回答
  •  难免孤独
    2021-01-14 16:25

    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 cq = cb.createQuery(IptExchangeratelines.class); Root exRateLine = cq.from(IptExchangeratelines.class); cq.where(cb.equal(cb.function("year", Integer.class,
      exRateLine.get(exRateLine_.currencyExchangeDate)), year) .and(cb.equal(cb.function("month", Integer.class, exRateLine.get(exRateLine_.currencyExchangeDate)), month)));

提交回复
热议问题