替换jpa中的月,年,日期功能 [英] Subsitute for Month,year,date functions in jpa

查看:216
本文介绍了替换jpa中的月,年,日期功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JPA中执行此查询.但是在执行此查询时遇到错误.如何在JPA中使用Month(),Year() 我们可以在原生SQL查询中使用它们,但是如何使用呢? 请帮助我.谢谢!!

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 it? Please help me.Thanks in advance!!

 select model from IptExchangeratelines model where model.iptExchangerate.id=:id and " +
                " month(model.currencyExchangeDate)=:month and year(model.currencyExchangeDate)=:year

推荐答案

很少有JPA实现具有对日期/时间函数的内置支持.

Few JPA implementations have built-in support for date/time functions.

  • EclipseLink

EclipseLink支持EXTRACT,允许将任何数据库支持的日期/时间部分的值设置为 从日期/时间中提取. EXTRACT函数与数据库无关,但是需要数据库支持.

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),但它需要EclipseLink 2.4.x

EXTRACT(YEAR, model.currencyExchangeDate), but it requires EclipseLink 2.4.x

FUNC 允许从JPQL调用数据库功能.它允许调用JPQL中不直接支持的任何数据库函数.

FUNC allows for a database function to be call from JPQL. It allows calling any database functions not supported directly in JPQL.

要调用DB函数MyFunc(a, b, c),请使用FUNC('MyFunc', a, b, c).

To call the DB function MyFunc(a, b, c) use FUNC('MyFunc', a, b, c).

您可以尝试FUNC('YEAR', currencyExchangeDate)调用年"功能.

You can try FUNC('YEAR', currencyExchangeDate) to call 'YEAR' function.

休眠

在HQL中,您可以具有日期功能YEAR(date)MONTH(date)DAY(date)以提取所需的详细信息. 支持的其他时间功能是HOUR(date)MINUTE(date)SECOND(date).

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

标准API

CriteriaBuilder#function():创建用于执行数据库功能的表达式.

CriteriaBuilder#function() : Create an expression for the execution of a database function.

CriteriaQuery<IptExchangeratelines> cq = cb.createQuery(IptExchangeratelines.class); Root<IptExchangeratelines> 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)));

CriteriaQuery<IptExchangeratelines> cq = cb.createQuery(IptExchangeratelines.class); Root<IptExchangeratelines> 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)));

这篇关于替换jpa中的月,年,日期功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆