我想比较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)
答案 0 :(得分:1)
由于您的列定义缺少@Temporal注释,因此您的Spring Data生成的JPQL查询很可能无法正常工作。规范说明此注释必须指定给任何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()));
答案 1 :(得分:0)
您不需要解析日期参数,请参阅此示例:
query.setParameter("publishDate", publishDate, TemporalType.TIMESTAMP);
您只需使用所需的参数类型设置参数,在本例中为时间戳,因此您可以使用TemporalType.TIMESTAMP。