如何在hibernate中执行我的查询

时间:2013-09-20 09:03:36

标签: sql hibernate

我的表格描述是

desc sensor_log_history;
+-------------+-----------------+------+-----+-------------------+-----------------------------+
| Field       | Type            | Null | Key | Default           | Extra                       |
+-------------+-----------------+------+-----+-------------------+-----------------------------+
| AutoPk      | int(4) unsigned | NO   | MUL | NULL              |                             |
| sensorName  | varchar(20)     | NO   |     | NULL              |                             |
| SensorValue | double(65,2)    | NO   |     | 0.00              |                             |
| timest      | timestamp       | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.00 sec)

我的查询是

SELECT AutoPk,Result1.sensorName,SensorValue,Result1.timest FROM(SELECT MAX(timest)AS timest,sensorName FROM sensor_log_history GROUP BY sensorName)AS Result1 INNER JOIN sensor_log_history ON Result1.timest = sensor_log_history.timest WHERE Result1.sensorName = sensor_log_history.sensorName;

如何使用hibernate执行此查询。 Hibernate应该返回sensor_log_history类型的对象列表。

1 个答案:

答案 0 :(得分:0)

只需在JAVA应用程序中创建一个POJO,然后使用hibernate将该表映射到该POJO。然后解雇HQL查询:“FROM”。