Mattia
Mattia

Reputation: 45

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. I'm using Java 8 on Spring Boot 2.2.5 (with Hibernate 5.4.12 and JPA 2.2.5).

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:

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. In fact if I query another entity with a LocalDate type parameter on an attribute of the same type it works as expected.

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.

Upvotes: 4

Views: 3410

Answers (1)

SternK
SternK

Reputation: 13111

Each function that you use in hql/jpql query should be declared in the hibernate dialect that you use. It looks like in your case the declaration of the date function looks somehow like this:

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:

/**
 * 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:

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.

Upvotes: 2

Related Questions