0

I have a class employee. It has a primary key ID and column called supervisorID. I want to display supervisor ID as the employees name, so I must do a join back to the same table. I'm using Spring Roo and Hibernate so I need to create an HQL query to do this but I can't figure out how to translate from SQL to HQL, the below is an example of the SQL query.

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; 
4

1 回答 1

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];
   }
于 2012-07-17T21:48:02.110 回答