JPA日期文字 [英] JPA date literal

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

问题描述

如何在不使用(类型化的)参数的情况下在JPA查询中表示日期?

如果日期确实是固定的(例如,1980年3月1日),则代码:

How represent a date in a JPA query, without using (typed) parameters?
If the date is really fixed (for example, 1 mar 1980), the code:

TypedQuery<MyEntity> q = em.createQuery("select myent from db.MyEntity myent where myent.theDate=?1", db.MyEntity.class).setParameter(1, d);

已设置:


Date d = new Date(80,Calendar.MARCH,1);

Date d = new Date(80, Calendar.MARCH, 1);

非常冗长,不是吗?我想在查询中嵌入1980/1/3。

is quite verbose, isn't it? I would like to embed 1980/1/3 into my query.

更新:
我将采样日期修改为1980/1/3,因为1980/1/1一直是模棱两可的。

UPDATE: I modified the sample date to 1980/1/3, because 1980/1/1 as it was, was ambiguous.

推荐答案

IIRC,您可以像在JDBC中一样在JPQL查询中使用日期文字,因此类似:

IIRC you can use date literals in JPQL queries just like you do it in JDBC, so something like:

// d at the beginning means 'date'
{d 'yyyy-mm-dd'} i.e. {d '2009-11-05'}

// t at the beginning means 'time'
{t 'hh-mm-ss'} i.e. {t '12-45-52'}

// ts at the beginning means 'timestamp'; the part after dot is optional
{ts 'yyyy-mm-dd hh-mm-ss.f'} i.e. {ts '2009-11-05 12-45-52.325'}

应该做这项工作(需要大括号和撇号)。

should do the work (the curly braces and apostrophes are required).

这篇关于JPA日期文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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