比较jpa中的日期
Posted
技术标签:
【中文标题】比较jpa中的日期【英文标题】:Comparing dates in jpa 【发布时间】:2016-03-07 07:13:17 【问题描述】:我正在尝试在两个日期之间的 postgresql 数据库中查找结果(在 ddbb 中描述为时间戳的字段)。
我有这三个必须满足目标的记录: 2016-03-04 00:00:00 2016-03-04 14:00:00 2016-03-04 10:56:00
final Calendar fechaMinima = Calendar.getInstance();
并设置我想要的参数。
jpa 列已定义:
@Column(name = "tim_ofrecim")
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date Tewslofr.timOfrecim;
使用 JPA,我尝试使用以下代码找到它们:
@NamedQuery(name = "ofr_query2", query = "SELECT COUNT (*) FROM Tewslofr ofr "
+ "WHERE ofr.id.codIdprodto =:codIdprodto "
+ "AND ofr.codUser =:codUser "
+ "AND ofr.timOfrecim BETWEEN :timMinimo AND :timMaximo")
public static Long getMethod(final String codProducto,
final String codUser, final Calendar fechaMinima,
final Calendar fechaMaxima)
return entityManager().createNamedQuery("ofr_query2", Long.class)
.setParameter("codIdprodto", codProducto)
.setParameter("codUser", codUser)
.setParameter("timMinimo", fechaMinima, TemporalType.TIMESTAMP)
.setParameter("timMaximo", fechaMaxima, TemporalType.TIMESTAMP)
.getSingleResult();
并获得此异常:
原因:java.lang.IllegalArgumentException:参数值[java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="欧洲/巴黎",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0 ,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2 ]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=1,DAY_OF_YEAR=67,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0 ,HOUR=8,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=434,ZONE_OFFSET=3600000,DST_OFFSET=0]] 不匹配类型 [java.util.Date]
对此的堆栈跟踪是:
org.springframework.dao.InvalidDataAccessApiUsageException: 参数值 [java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe /Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0, startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2] ],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=1,DAY_OF_YEAR=67,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0, HOUR=9,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=170,ZONE_OFFSET=3600000,DST_OFFSET=0]] 不匹配类型 [java.util.Date];嵌套异常是 java.lang.IllegalArgumentException: 参数值 [java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Paris ",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=欧洲/巴黎,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode= 2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]], firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=1,DAY_OF_YEAR=67,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR= 9,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=170,ZONE_OFFSET=3600000,DST_OFFSET=0]] 不匹配类型 [java.util.Date] 在 org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:301) 在 org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15) 在 com.bbva.arq.front.spring.ewsl.ofrecimientos.api.v1.domain.Tewslofr.getCuentaOfrecimientosAgente_aroundBody2(Tewslofr.java:49) 在 com.bbva.arq.front.spring.ewsl.ofrecimientos.api.v1.domain.Tewslofr.getCuentaOfrecimientosAgente(Tewslofr.java:1) 在 com.bbva.arq.front.spring.ewsl.ofrecimientos.api.v1.daos.ofrecimientos.impl.OfrecimientosDAOImpl.getMethod(OfrecimientosDAOImpl.java:52)... 引起:java.lang.IllegalArgumentException:参数值[java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Paris ",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=欧洲/巴黎,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode= 2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]], firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=1,DAY_OF_YEAR=67,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR= 9,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=170,ZONE_OFFSET=3600000,DST_OFFSET=0]] 不匹配类型 [java.util.Date] 在 org.hibernate.ejb.AbstractQueryImpl.registerParameterBinding(AbstractQueryImpl.java:360) 在 org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:414)
如果我更改为 DATE 而不是 TIMESTAMP:
public static Long getMethod(final String codProducto,
final String codUser, final Calendar fechaMinima,
final Calendar fechaMaxima)
return entityManager().createNamedQuery("ofr_query2", Long.class)
.setParameter("codIdprodto", codProducto)
.setParameter("codUser", codUser)
.setParameter("timMinimo", fechaMinima.getTime(), TemporalType.DATE)
.setParameter("timMaximo", fechaMaxima.getTime(), TemporalType.DATE)
.getSingleResult();
代码返回 0。
任何帮助将不胜感激。
最诚挚的问候
【问题讨论】:
你能得到整个堆栈跟踪吗?我感觉异常是由 .getTime() 方法引发的,而不是由 JPA 我已经用堆栈跟踪编辑了帖子 【参考方案1】:我仍然不知道为什么第一个选项不起作用,但我已经使它与日期选项一起工作。这个选项是正确的,但返回 0 是因为 de codIdprodto,而不是因为日期。
【讨论】:
以上是关于比较jpa中的日期的主要内容,如果未能解决你的问题,请参考以下文章
Spring JPA 标准构建器大于:我可以传递字符串值以比较数据库中的数字类型吗