【问题标题】:How can I make a HQL using Instant from java.time如何使用 java.time 中的 Instant 制作 HQL
【发布时间】:2017-11-09 20:55:04
【问题描述】:

我正在使用 spring-jpa 开发一个小项目。我决定使用 java.time 来处理日期问题。在我不得不处理 HQL 之前,一切正常。

所以我正在尝试做这个查询:

@Query("SELECT sensor "
            + "FROM TrashSensor sensor "
            + "join sensor.measurementList measurement "
            + "WHERE sensor.id = 100 AND measurement.instantOfMeasure > '2017-01-01'")
    public TrashSensor findTrashSensorByIdCustom();

measurement.instantOfMeasure 的类型是来自 java.time 的 Instant。

所以我的 WHERE 子句的最后一个 AND 总是返回 true,所以我没有得到我需要的过滤器。

如何使用 HQL 和 java.time 进行比较? Hibernate 支持吗?

【问题讨论】:

  • 创建一个Instant 对象并将其作为参数传入...

标签: spring hibernate jpa hql java-time


【解决方案1】:

你可以用

开发它

1 路 ->(在 TrashSensorRepository 接口中)

@Query("SELECT sensor "
            + "FROM TrashSensor sensor "
            + "join sensor.measurementList measurement "
            + "WHERE sensor.id =:sensorId AND measurement.instantOfMeasure > :instantOfMeasure")
public TrashSensor findTrashSensorByIdCustom(@Param("sensorId") sensorId, @Param("instantOfMeasure") Instant instantOfMeasure);

java.time 包中的类型直接映射到对应的 SQL 类型(Java 8 起)

  • Lo​​calDate 映射到 DATE
  • Lo​​calTime 和 OffsetTime 映射到 TIME
  • Instant、LocalDateTime、OffsetDateTime 和 ZonedDateTime 映射到 TIMESTAMP

2 路 ->(如果您已创建自定义实用程序类以动态运行 HQL)

// Utility for run custom dynamic HQL query
@Service
@Transactional
public class HibernateQueryService {

    private final Logger log = LoggerFactory.getLogger(HibernateQueryService.class);
    private JpaContext jpaContext;

    public List executeHibernateQuery(String sql, Class entity){
        log.debug("HibernateQuery executing: "+sql);
        Session session = jpaContext.getEntityManagerByManagedType(Article.class).unwrap(Session.class);
        return session.createQuery(sql, entity).getResultList();
    }
}
// Custom converter
public class CustomConverter{    

public static Timestamp toTimestamp(Instant time){
        return time != null ? Timestamp.from(time) : null;
    }

    public static Instant toInstant(Timestamp timestamp){
        return timestamp != null ? timestamp.toInstant() : null;
    }

}
// Solution

public class Solution2{


    public static makeIt(){

        boolean isFirst = true;

        String query = "SELECT sensor "
            + "FROM TrashSensor sensor "
            + "join sensor.measurementList measurement ";            

        if(sensor.id != null){
            query += isFirst ? "WHERE " : "AND ";
            query += "sensor.id="+sensor.id+" ";
            isFirst = false;
        }

        if(measurement.instantOfMeasure != null){
            query += isFirst ? "WHERE " : "AND ";
            query += "measurement.instantOfMeasure > "+toTimestamp(measurement.instantOfMeasure)+" ";            
        }

        List<TrashSensor> hibernateQueryService.executeHibernateQuery(query, TrashSensor.class);

    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-12
    • 2016-01-20
    • 1970-01-01
    • 2016-07-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多