如何在不使用(类型化)参数的情况下表示 JPA 查询中的日期?
如果日期确实是固定的(例如 1980 年 3 月 1 日),则代码:
TypedQuery<MyEntity> q = em.createQuery("select myent from db.MyEntity myent where myent.theDate=?1", db.MyEntity.class).setParameter(1, d);
设置:
日期 d = new Date(80, Calendar.MARCH, 1);
相当冗长,不是吗?我想将 1980/1/3 嵌入到我的查询中。
更新:
我将样本日期修改为 1980/1/3,因为 1980/1/1 本身是不明确的。
IIRC 您可以在 JPQL 查询中使用日期文字,就像在 JDBC 中一样,例如:
// 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'}
应该完成这项工作(需要花括号和撇号)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)