比较日期jpa

时间:2016-03-07 07:13:17

标签: jpa

我试图在两个日期之间的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 =& #34;欧/巴黎",偏移= 3600000,dstSavings = 3600000,useDaylight =真,过渡= 184,lastRule = java.util.SimpleTimeZone中[ID =欧洲/巴黎,偏移= 3600000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 2,startMonth = 2,朝九特派= -1,startDayOfWeek = 1,开始时间= 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endday指定= -1,一个endDayOfWeek = 1,结束时间= 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 =&# 34;欧洲/巴黎",偏移= 3600000,dstSavings = 3600000,useDaylight =真,过渡= 184,lastRule = java.util.SimpleTimeZone中[ID =欧洲/巴黎,偏移= 3600000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 2,startMonth = 2,朝九特派= -1,startDayOfWeek = 1,开始时间= 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endday指定= -1,一个endDayOfWeek = 1,结束时间= 360万,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 ="欧洲/巴黎",偏移= 3600000,dstSavings = 3600000,useDaylight =真,过渡= 184,lastRule = java.util.SimpleTimeZone中[ID =欧洲/巴黎,偏移= 3600000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 2,startMonth = 2,朝九特派= -1,startDayOfWeek = 1,开始时间= 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endday指定= -1,一个endDayOfWeek = 1,结束时间= 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)       at 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 ="欧洲/巴黎",偏移= 3600000,dstSavings = 3600000,useDaylight =真,过渡= 184,lastRule = java.util.SimpleTimeZone中[ID =欧洲/巴黎,偏移= 3600000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 2,startMonth = 2,朝九特派= -1,startDayOfWeek = 1,开始时间= 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endday指定= -1,一个endDayOfWeek = 1,结束时间= 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。

任何帮助将不胜感激。

最亲切的问候

1 个答案:

答案 0 :(得分:0)

我仍然不知道为什么第一个选项不起作用,但我已经使用了Date选项。这个选项是正确的,但由于de codIdprodto而返回0,而不是因为日期。