我有一名班级员工。它有一个主键ID和名为supervisorID的列。我想显示主管ID作为员工姓名,所以我必须联系回同一个表。我正在使用Spring Roo和Hibernate所以我需要创建一个HQL查询来执行此操作,但我无法弄清楚如何从SQL转换为HQL,下面是SQL查询的示例。
SELECT e.ID, e.name AS Employee, s.name AS Supervisor
FROM employee e
INNER JOIN employee s
ON s.ID = e.supervisorID
ORDER BY e.ID;
答案 0 :(得分:0)
看起来你想要一个标量查询。
List<Object[]> rows = getEntityManager().createQuery(
"select e.id, e.name, s.name "
+ " from Employee e join fetch e.supervisor s "
+ " order by e.id").getResultList();
for ( Object[] row: rows ) {
// Construct an instance (or whatever) with the values
Long id = (Long) row[0];
String eName = (String) row[1];
String sName = (String) row[2];
}