如何用HQL语言编写SQL查询?

时间:2015-05-28 21:19:47

标签: sql hql inner-join

我试图写这个:

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

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

有什么想法吗? 感谢。

1 个答案:

答案 0 :(得分:1)

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

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

看起来应该更像这样,其中连接的实体是属性名称,而不是表名。例如,您的部门实体将拥有一个名为Employee的{​​{1}}类型的集合。

employees