【问题标题】:Datetime expression in JPA by passing as parameterJPA中的日期时间表达式作为参数传递
【发布时间】:2014-12-03 00:52:14
【问题描述】:

我想比较 JPA 中的日期时间函数。只是想知道如何通过它。顺便说一句,我对 JPA 很陌生。我的输入日期将如下所示 28/12/2014 16:20 我将其解析为日期

我必须将此作为参数传递,但数据库表 (MS SQL) 包含日期时间格式为 2014-12-28 16:20:38.107

这是我的代码。

在@Entity 下(我无法编辑此代码,因为我没有权限这样做。无论如何其他方法正在访问此实体,我只是重用它)

    @Column(name = "PublishDate", nullable = false)
    private Date publishDate;

我的实际代码

    Date date = simpleDateFormat.parse(s);
    Message.findByPublishDate(date);

在使用Debug=true 运行上述代码后,我看到sql 查询中传递的参数为: messages0_.PublishDate=? 这意味着它甚至没有记录日期,我得到以下异常

select operations0_.OperationsMessageID as Operatio1_10_, operations0_.Content as Content2_10_, 
operations0_.CreatedDate as CreatedD3_10_, operations0_.ExpiryDate as ExpiryDa4_10_, 
operations0_.PublishDate as PublishD5_10_, operations0_.Status as Status6_10_, operations0_.Title 
as Title7_10_, operations0_.OperationsMessageTypeID as Operatio8_10_ from OperationsMessage 
operations0_ where operations0_.PublishDate=?

异常详情:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:172)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:155)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy44.findByPublishDate(Unknown Source)

【问题讨论】:

  • 请显示查询
  • 史蒂夫,感谢添加查询
  • 即生成的SQL,我们需要查看JPQL查询。还有PublishDate的SQL列类型是什么。
  • public OperationsMessage findByPublishDate(Date publishDate); - 就是这个,我正在使用 crud
  • 那不是查询,只是方法调用。显示此方法的源代码

标签: java spring hibernate datetime


【解决方案1】:

很有可能您的 Spring Data 生成的 JPQL 查询无法正常工作,因为您的列定义缺少 @Temporal 注释。规范规定此注释必须指定给任何java.util.Date 或java.util.Calendar 类型的列。

我知道你说过你不能改变它,但我认为没有选择:

    @Temporal(TemporalType.DATE)
    @Column(name = "PublishDate", nullable = false)
    private Date publishDate;

如果物理数据库列是时间戳 SQL 类型,您可能需要 TemporalType.TIMESTAMP。

或者,该列已指定为java.sql.Date。如果是这种情况,那么您可能需要使用该日期类型作为 findByPublishDate 的参数,从您的 java.util.Date 转换:

    Message.findByPublishDate(new java.sql.Date(date.getTime()));

【讨论】:

    【解决方案2】:

    你不需要解析你的日期参数,看这个例子:

    query.setParameter("publishDate", publishDate, TemporalType.TIMESTAMP);
    

    您只需使用您想要的参数类型设置参数,在本例中为时间戳,因此您可以使用 TemporalType.TIMESTAMP 。

    【讨论】:

    • 是的,您只需要更改您的查询对象并在您的 jpql 语句中设置参数。在 findByPublishDate 方法中显示代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多