如何用HQL语言写一个SQL查询?

How write a SQL query in HQL language?

我正在尝试写这个:

select s.to_date 'Fecha Fin', concat(e.first_name, " ", e.last_name) as 'Full Name', t.title as 'Title', s.salary as 'Salary'
from dept_emp as d
join employees as e on d.emp_no = e.emp_no
join salaries s on s.emp_no = e.emp_no
join titles t on t.emp_no = e.emp_no
where d.dept_no = "d007"
order by e.emp_no, s.to_date desc;

关于 HQL 语言。这是我写的:

session.createQuery("select s.to_date, concat(e.first_name,'', e.last_name) as FullName, t.title as Title, s.salary as Salary
from Department as d
inner join Employees as e where d.emp_no = e.emp_no  
inner join Salaries s where s.emp_no = e.emp_no
inner join Titles t where t.emp_no = e.emp_no
where s.to_date = '9999-01-01' 
AND d.dept_no = 'd007' 
order by e.emp_no, s.to_date desc") as Department

但是我遇到了这个错误:

0 [main] ERROR org.hibernate.hql.PARSER  - line 10:57: unexpected token: inner

我知道语法错误不是唯一的错误,但我找不到这个问题的任何答案。

有什么想法吗? 谢谢。

这不是有效的 HQL。只有一个 where 子句允许。而且您不告诉 Hibernate 如何在查询中加入您的实体。您告诉 Hibernate 在映射实体时如何加入它们。

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

看起来应该更像这样,其中连接的实体是 属性 个名称,而不是 table 个名称。例如,您的 Department 实体将有一个名为 employees.

的类型 Employee 的集合
session.createQuery("select s.to_date, " + 
    " concat(e.first_name,'', e.last_name) as FullName, " + 
    " t.title as Title, s.salary as Salary " +
    " from Department as d " + 
    " inner join d.employees as e " +
    " inner join e.salary as s " +
    " inner join e.title t " +
    " where s.to_date = '9999-01-01' " + 
    " AND d.dept_no = 'd007' " +
    " order by e.emp_no, s.to_date desc");