【发布时间】: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