如何构建 Hibernate 查询来生成时间序列图数据

发布于 2024-11-29 15:02:48 字数 1174 浏览 0 评论 0原文

存在一个 Hibernate 实体

WeatherStatistic  

,其中包含给定时间的大气数据快照。

如果 WeatherStatistic 只有一种大气测量类型,例如。温度:

| time   | temperature |  
 -------- -------------   
| 09:00  |    10       |  
| 10:00  |    20       |  
| 11:00  |    15       |  

然后我可以编写一个 HQL 查询:

select 'temperature' time temperature from WeatherStatistic

它将返回

'temperature'  09:00   10
'temperature'  10:00   20
'temperature'  11:00   15

第一列是字符串文字,后续列是时间和温度值。

如果表如下:

| time   | temperature | humidity |  
 -------- ------------- ----------  
| 09:00  |    10       |   20     |
| 10:00  |    20       |   15     |
| 11:00  |    15       |   30     |

是否可以编写一个查询(Criteria/HQL/SQL)来生成以下结果?

'temperature'  09:00   10
'temperature'  10:00   20
'temperature'  11:00   15
'humidity'     09:00   20
'humidity'     10:00   15
'humidity'     11:00   30

如果可以做到这一点,那么是否可以提供灵活性以允许提取字段值的任意组合,即:温度或湿度,或两者?

该数据将用于动态生成时间序列图。我正在使用 Jasper 的 Hibernate 数据源,它接收 HQL 查询字符串并将查询结果直接提供给 Jasper,然后 Jasper 输出生成的图形图像。

There exists a Hibernate entity

WeatherStatistic  

which contains snapshots of atmospheric data at given times.

If the WeatherStatistic had only 1 type of atmospheric measurement, eg. temperature:

| time   | temperature |  
 -------- -------------   
| 09:00  |    10       |  
| 10:00  |    20       |  
| 11:00  |    15       |  

Then I could write an HQL query:

select 'temperature' time temperature from WeatherStatistic

which would return

'temperature'  09:00   10
'temperature'  10:00   20
'temperature'  11:00   15

The first column is a string literal, the subsequent columns are time and the temperature value.

If the table is as follows:

| time   | temperature | humidity |  
 -------- ------------- ----------  
| 09:00  |    10       |   20     |
| 10:00  |    20       |   15     |
| 11:00  |    15       |   30     |

Is it possible to write a query (Criteria/HQL/SQL) to produce the results below?

'temperature'  09:00   10
'temperature'  10:00   20
'temperature'  11:00   15
'humidity'     09:00   20
'humidity'     10:00   15
'humidity'     11:00   30

If this can be done, is it then possible to provide the flexibility to allow any combination of fields value to be pulled out, ie: either temperature or humidity, or both?

The data is to be used for dynamic generation of time series graphs. I am working with a Hibernate data source for Jasper which takes in a HQL query string and feeds the query results directly up to Jasper which then outputs the generated graph images.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-12-06 15:02:48

在 SQL 中,您可以使用联合:

select * from 
    (select 'temperature' as label, time, temperatureas value from weatherstatistic
     union
     select 'humidity' as label, time, humidity as value from weatherstatistic)
order by label, time

在将其传递给 JasperReports 之前,您必须动态构造此查询。

由于 HQL 不支持联合,因此我看不到在 HQL 中实现联合的方法。

您还可以之前获取 Java 代码中的数据(使用简单的 HQL 查询),构建包含标签、时间和值的 Java Bean 列表,并将它们用作 JRBeanCollectionDataSource

In SQL, you could use a union:

select * from 
    (select 'temperature' as label, time, temperatureas value from weatherstatistic
     union
     select 'humidity' as label, time, humidity as value from weatherstatistic)
order by label, time

You will have to construct this query dynamically before passing it to JasperReports.

Since HQL doesn't support union, I don't see a way to do it in HQL, though.

You could also fetch the data in your Java code before (using a simple HQL query), build a list of Java Beans containing a label, time and value, and use these as a JRBeanCollectionDataSource.

情绪少女 2024-12-06 15:02:48

我想我应该发布我在考虑 JB Nizet 的建议后实际提出的解决方案!

我通过编写一个接受 HQL 查询列表的自定义 Jasper-Hibernate 数据源解决了这个问题。在幕后,该数据源串行执行查询(有效地模拟联合)并将结果连接在一起,以便 Jasper 可以顺序访问它们。

Thought I should post the solution I actually came up after considering JB Nizet's suggestions!

I solved the problem by writing a custom Jasper-Hibernate data source that accepts a List of HQL queries. Under the hood, this data source executes the queries in series (effectively simulating a union) and concatenates the results together so that Jasper can access them sequentially.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文