2015-05-28 62 views
0

我試圖寫:如何在HQL語言中編寫SQL查詢?

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 

我知道,語法錯誤不是唯一的錯誤,但我無法找到這個問題的任何回答。

有什麼想法? 謝謝。

+0

也許是因爲您在工資和職位後忘記了關鍵字'as' –

+0

謝謝!但事實並非如此..我想這個「as」可以在HQL中消除......我證明了這一點,錯誤仍然存​​在。 –

+0

然後ist必須是''as'在'部門中作爲d' ;-)自從使用hibernate以來已經很長時間了。我記得有一個'as'的問題。但我不知道到底在哪裏。 –

回答

1

這不是有效的HQL。只有一個where子句允許。而且你不會告訴Hibernate如何在查詢中加入你的實體。你告訴Hibernate當你映射它們時如何加入你的實體。

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

應該看起來更像是這個,這裏加入了實體屬性的名稱,而不是表名。例如,您的部門實體將擁有名爲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"); 
+0

謝謝!這是正確的答案.. –