【问题标题】:Hibernate Native SQL returns some nulls instead of partially filled objectsHibernate Native SQL 返回一些空值而不是部分填充的对象
【发布时间】:2021-06-10 13:13:40
【问题描述】:

我有以下(postgress)SQL 查询:

WITH cal AS(
  SELECT ts AS t_begin, ts + '6hours'::interval AS t_end 
  FROM generate_series('2021-05-09 00:00:00'::timestamp 
                      , '2021-06-15 09:51:34', '6hours'::interval) ts 
  ) 
  SELECT d.variable_id, cal.t_end AS date_time, d.sample_count, d.sample_period_ms, MIN(d.min_value) AS min_value, MAX(d.max_value) AS max_value, AVG(d.value) AS value 
  FROM cal 
  LEFT JOIN public.fp_data d 
      ON d.date_time >= cal.t_begin 
      AND d.date_time < cal.t_end 
      AND variable_id = 15
  GROUP BY cal.t_end, d.variable_id, d.sample_count, d.sample_period_ms 
  ORDER BY cal.t_end

它所做的是每天创建一系列 6 小时并从中获取平均值。结果可能如下所示:


variable_id date_time sample_count sample_period_ms min_value max_value value
15 2021-06-06 06:00:00 120 59577 -1.4960686 1.1995025 0.30439844254136744
15 2021-06-06 12:00:00 120 59577 -1.4887594 1.1997863 0.30570657099738263
15 2021-06-06 18:00:00 120 59577 -1.4972655 1.1999407 0.30465021305485984
15 2021-06-07 00:00:00 120 59577 -1.4703176 1.1985717 0.30615092198218197
15 2021-06-07 06:00:00 120 59577 -1.4983453 1.1998215 0.3049584258111712
15 2021-06-07 12:00:00 120 59577 -1.4996965 1.1996177 0.3047593224032149
15 2021-06-07 18:00:00 120 59577 -1.4949534 1.1998252 0.30591585460444787
15 2021-06-08 00:00:00 120 59577 -1.4997886 1.1995926 0.30432341914644556
15 2021-06-08 06:00:00 120 59577 -1.4956167 1.1996672 0.3085149774948756
15 2021-06-08 12:00:00 120 59577 -1.4986398 1.1998078 0.30561149754247613
15 2021-06-08 18:00:00 120 59577 -1.499255 1.1990205 0.3064040885648123
15 2021-06-09 00:00:00 120 59577 -1.4864591 1.1998134 0.3057553283664403
NULL 2021-06-09 06:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-09 12:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-09 18:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-10 00:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-10 06:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-10 12:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-10 18:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-11 00:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-11 06:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-11 12:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-11 18:00:00 NULL NULL NULL NULL NULL
NULL 2021-06-12 00:00:00 NULL NULL NULL NULL NULL

我在我的 JPA 界面中实现了这个查询,如下所示:

@Transactional
public interface FloatingPointDataRepos extends CrudRepository<FloatingPointData, Integer> {
    
    List<FloatingPointData> findAllByVariable_IdAndDateTimeBetween(Integer variableId, Timestamp startTimeFrame, Timestamp endTimeFrame);

    @Query(value = "WITH cal AS( "+
                        "SELECT ts AS t_begin, ts + '6hours'\\:\\:interval AS t_end "+
                        "FROM generate_series(:startTimeFrame\\:\\:timestamp "+
                                            ", :endTimeFrame, '6hours'\\:\\:interval) ts "+
                        ") "+
                        "SELECT d.variable_id, cal.t_end AS date_time, d.sample_count, d.sample_period_ms, MIN(d.min_value) AS min_value, MAX(d.max_value) AS max_value, AVG(d.value) AS value "+
                        "FROM cal "+
                        "LEFT JOIN public.fp_data d "+
                            "ON d.date_time >= cal.t_begin "+
                            "AND d.date_time < cal.t_end "+
                            "AND variable_id = :variableId " +
                        "GROUP BY cal.t_end, d.variable_id, d.sample_count, d.sample_period_ms "+
                        "ORDER BY cal.t_end", nativeQuery = true)
    List<FloatingPointData> findAllByVariableId(Integer variableId, Timestamp startTimeFrame, Timestamp endTimeFrame);

}

代码有效,但问题是它为未设置 variable_id 和其他值的行返回 null。

它现在返回的示例如下:

object
object
object
object
object
object
object
object
object
object
null
null
null
null
null
null

我希望它始终在对象中返回 date_time,就像您在上表中看到的那样。

我该如何解决这个问题?

【问题讨论】:

    标签: java spring postgresql hibernate jpa


    【解决方案1】:

    variable_id 是主键。当 variable_id 为 null 时,Hibernate 返回 null 而不是 object。

    解决方法是将SELECT d.variable_id 更改为SELECT :variableId AS variable_id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2023-04-11
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多