Home > OS >  HQL Expects java.time.duration even though method-head and usage suggest LocalDateTime
HQL Expects java.time.duration even though method-head and usage suggest LocalDateTime

Time:01-26

I am writing a query within a JpaRepository whicht take a String and 3 LocalDateTimes as parameters. Within the query I first compare the String like an Id and afterwards I use a different Column of the corresponding Entity to create LocalDateTimes using appropriate operators for Hibernate 6.

The Application starts up normal but when i call the query I get the following Error:

Argument [2023-01-23T11:43:59] of type [java.time.LocalDateTime] did not match parameter type [java.time.Duration (n/a)]

The Argument obviously got parsed correctly by the Restcontroller but Hibernate does not seem to create the query as expected.

The following is the Code for the Repository and the query in question:

public interface ExchangePairRepository extends JpaRepository<MyEntity, Long> {

@Query("SELECT ep.unit FROM MyEntity ep WHERE ep.id= :id AND ((ep.context = 'start' AND ((ep.unit = 'day' AND (:start  ep.duration day) > :now) "
          "OR (ep.unit = 'month' AND (:start  ep.duration month) > :now))) OR (ep.context = 'end' AND ((ep.unit = 'day' AND (:now   ep.duration day) > :end) "
          "OR (ep.unit = 'month' AND (:now   ep.duration month) > :end)) ))")
List<String> findViable(@Param("matNrOrig") String id, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end,
        @Param("now") LocalDateTime now);

}

Below is the Entity which i being used for the query:

@Entity
@Table(name = "my_entity")
@Data
public class MyEntity{
    (...)

@Column(name = "id_orig")
private String idOrig;

´
@Column(name = "id_target")
private String idTarget;

@Column(name = "context")
private String context;

@Column(name = "duration")
private int duration;

@Column(name = "unit")
private String unit;

}

Is this a bug or am I doing something wrong? Any help is much appreciated.

I am using Hibernate 6.1.6.Final and Spring Boot 3.0.1 with Java 17

CodePudding user response:

This is a bug in the new parameter type inference logic. I can reproduce with just this query:

session.createQuery("select :dt   1 day")
        .setParameter("dt", LocalDateTime.now())
        .getSingleResult();

I have opened an issue for you here: https://hibernate.atlassian.net/browse/HHH-16102

  • Related