Java 如何在Hibernate原始sql查询中使用LocalDateTime?

Java 如何在Hibernate原始sql查询中使用LocalDateTime?,java,spring-boot,hibernate,jpa,Java,Spring Boot,Hibernate,Jpa,我正在尝试使用hibernate使用原始sql生成pojo: // the query code Session session = entityManager.unwrap(Session.class); UserDetailResponse detail = (UserDetailResponse) session.createSQLQuery(sql) .setParameter("uid", id)

我正在尝试使用hibernate使用原始sql生成pojo:

// the query code
Session session = entityManager.unwrap(Session.class);
UserDetailResponse detail = (UserDetailResponse) session.createSQLQuery(sql)
                        .setParameter("uid", id)
                  .setResultTransformer(Transformers.aliasToBean(UserDetailResponse.class)).uniqueResult();

// the pojo
    public class UserDetailResponse {
        private int id;
        private BigDecimal totalSettleAmount;
        private LocalDateTime lastSettleTime;
    
        public BigDecimal getTotalSettleAmount() {
            return totalSettleAmount;
        }
    
        public void setTotalSettleAmount(BigDecimal totalSettleAmount) {
            this.totalSettleAmount = totalSettleAmount;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public LocalDateTime getLastSettleTime() {
            return lastSettleTime;
        }
    
        public void setLastSettleTime(LocalDateTime localDateTime) {
            this.lastSettleTime = localDateTime;
        }
    }
但是,hibernate提出了一个非法的ArgumentException:

ERROR 17676 --- [nio-8080-exec-7] o.h.p.access.spi.SetterMethodImpl        : HHH000123: IllegalArgumentException in class: io.loremipsum.test.dto.UserDetailResponse, setter method of property: lastSettleTime
ERROR 17676 --- [nio-8080-exec-7] o.h.p.access.spi.SetterMethodImpl        : HHH000091: Expected type: java.time.LocalDateTime, actual value: java.sql.Timestamp
根据这一点,hibernate从5.2开始就支持LocalDatetime,而我使用的版本是5.4.20 Final


有没有我错过的配置?

Hibernate没有,JDBC没有。您正在使用带有普通映射的SQL,因此不受支持。我建议只编写一个JPQL并支持它。@M.Deinum其他类型,如integer、string、bigdecimal,可以很好地使用这种映射。因为JDBC本来就支持这些类型,而
java.time
类则不支持。由于Hibernate/JPA在这里映射时被忽略了(这是通常使用
AttributeConverter
进行转换的情况),因此它不起作用。