[英]The java.time.LocalDate parameter processes as java.util.Date in hibernate query
I'm trying to pass a java.time.LocalDate
parameter to a named query but Hibernate wrongly expected parameter of java.util.Date
type.我正在尝试将
java.time.LocalDate
参数传递给命名查询,但 Hibernate 错误地预期了java.util.Date
类型的参数。 I'm using Java 8 on Spring Boot 2.2.5 (with Hibernate 5.4.12 and JPA 2.2.5).我在 Spring Boot 2.2.5(使用 Hibernate 5.4.12 和 JPA 2.2.5)上使用 Java 8。
Exception:例外:
Exception - org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [2020-03-30] did not match expected type [java.util.Date (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [2020-03-30] did not match expected type [java.util.Date (n/a)]
DAO method: DAO方法:
java.time.LocalDate date = LocalDate.now(); // example date
result = entityManager.createNamedQuery("MyTableEntity.query", MyTableEntity.class)
.setParameter("date", date)
.setMaxResults(1)
.getSingleResult();
Named query:命名查询:
@NamedQuery(name = "MyTableEntity.query", query = "SELECT o FROM MyTableEntity o WHERE date(o.timestamp) = :date")
Entity:实体:
@Entity
@Table(name = "my_table")
public class MyTableEntity implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "time_control")
private java.time.OffsetDateTime timestamp;
private static final long serialVersionUID = 1L;
// constructors...
// getters and setters...
}
I think the problem (bug?) is in the named query where the cast from Timestamp
to Date
generate a java.util.Data
type.我认为问题(错误?)出在命名查询中,其中从
Timestamp
到Date
生成java.util.Data
类型。 In fact if I query another entity with a LocalDate
type parameter on an attribute of the same type it works as expected.事实上,如果我在相同类型的属性上使用
LocalDate
类型参数查询另一个实体,它会按预期工作。
However I resolved this by passing java.sql.Date.valueOf(date)
as parameter, but I want to know why Hibernate did not resolve LocalDate
as I expected.但是我通过传递
java.sql.Date.valueOf(date)
作为参数解决了这个问题,但我想知道为什么 Hibernate 没有像我预期的那样解析LocalDate
。
Each function that you use in hql/jpql query should be declared in the hibernate dialect that you use.您在 hql/jpql 查询中使用的每个函数都应该在您使用的休眠方言中声明。 It looks like in your case the declaration of the
date
function looks somehow like this:在您的情况下,
date
函数的声明看起来像这样:
import org.hibernate.type.StandardBasicTypes;
public YourDialect {
// ...
registerFunction( "date", new StandardSQLFunction( "date", StandardBasicTypes.DATE ) );
}
and below you can see how the StandardBasicTypes.DATE
is defined:在下面你可以看到
StandardBasicTypes.DATE
是如何定义的:
/**
* The standard Hibernate type for mapping {@link java.util.Date} ({@link java.sql.Date}) to JDBC
* {@link java.sql.Types#DATE DATE}.
*
* @see TimeType
*/
public static final DateType DATE = DateType.INSTANCE;
That is why you got the mentioned in your question exception.这就是为什么你在你的问题例外中提到了。 You can change this by redefining the
date
function in the following way:您可以通过以下方式重新定义
date
函数来更改此设置:
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.LocalDateType;
public class MyDialect extends SomeHibernateDialect
{
public MyDialect()
{
super();
registerFunction( "date", new StandardSQLFunction( "date", LocalDateType.INSTANCE ) );
}
}
And then use this dialect.然后用这个方言。 After that you initial query will work.
之后,您的初始查询将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.