简体   繁体   English

HQL查询以使用左外部联接与其自身联接表

[英]HQL query to join table with itself with left outer join

I want to left outer join a table with itself so that I will get additional columns. 我想让外部与其自身联接表,以便获得更多的列。

I have a table that collect serial number and action as a log. 我有一张收集序列号和操作作为日志的表。 I want join two actions a1 and a2 based on the serial number. 我想根据序列号加入两个动作a1和a2。 The returned result is expected to have serialNumber, a1 and a2. 返回的结果应具有serialNumber,a1和a2。 If a2 doesn't exist it will be null. 如果a2不存在,它将为null。 When I use where statement it will only return serialNumber which has both a1 and a2. 当我使用where语句时,它将仅返回同时具有a1和a2的serialNumber。

"select l.serialNumber, l.time, s.time "
+ "from Log as l left outer join Log as s "
+ "ON s.serialNumber = l.serialNumber "
+ "where s.action = 'a2' AND l.action= 'a1' "
+ "AND l.time >= '" + start.getTime() + "' "
+ "AND l.time <= '" + end.getTime() + "' "

I am getting an exception while trying to run the above query: org.hibernate.TransactionException. 我在尝试运行上述查询时遇到异常:org.hibernate.TransactionException。

Your HQL is not valid, you don't need (can) specify the JOIN columns, this is already need to be mapped in your entities. 您的HQL无效,您不需要(可以)指定JOIN列,这已经需要在您的实体中进行映射。

Try: 尝试:

"select l.serialNumber, l.time, s.time "
+ "left join l.serialNumber s "
+ "where s.action = 'a2' AND l.action= 'a1' "
+ "AND l.time >= '" + start.getTime() + "' "
+ "AND l.time <= '" + end.getTime() + "' "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM