2013-05-07 143 views
0

請如何寫這個簡單的工作選擇,在Hibernate查詢,或標準休眠加入

SELECT * FROM `call` AS c JOIN `event_customer` AS e ON c.customer_id = e.customer_id WHERE e.dgdelete = FALSE AND c.eventruletype_id = 1 AND e.event_id = 74 AND c.callactivity_id = 3 AND e.calltype = 2 

我發現已經有很多建議,但沒有任何幫助我呢。

它可能是什麼與Hibernate映射?

謝謝。

PS: 這是行不通的

query = session.createQuery("select c from " + Call.class.getSimpleName()+ " as c " + EventCustomer.class.getSimpleName()+ " as e where e.customer_id = c.customer_id and e.event_id = 74"); 

也是這個工作不

query = session.createQuery("select c from " + Call.class.getSimpleName()+ " as c join " + EventCustomer.class.getSimpleName()+ " as e where e.customer_id = c.customer_id and e.event_id = 74"); 

而且這個

query = session.createQuery("select c from " + Call.class.getSimpleName()+ " as c join " + EventCustomer.class.getSimpleName()+ " as e on e.customer_id = c.customer_id where e.event_id = 74"); 

而且這個

query = session.createQuery("select c from " + Call.class.getSimpleName()+ " as c left join " + EventCustomer.class.getSimpleName()+ " as e on e.customer_id = c.customer_id where e.event_id = 74"); 

,並在某些情況下,我得到這個錯誤

+0

您以何種方式查詢不起作用?會發生什麼與你的預期?什麼是數據庫模式和Java數據模型? – Adrian 2013-05-07 16:08:09

回答

0

這應該工作。 (用逗號替換JOIN)

Query query = session.createQuery("select c from " + Call.class.getSimpleName() + " as c , " + EventCustomer.class.getSimpleName() + " as e where e.customer.id = c.customer.id and c.event.id = 74"); 
2

不要連擊,而使用標準:

Criteria crit = session.createCriteria(Call.class, "c") 
    .createCriteria("eventCustomerDto","e") 
    .add(Restrictions.eq("event_id",74)); 
List<Call> list=crit.list(); 

的代碼可能有錯別字,但多數民衆贊成這個想法。另外,對於我的例子,我認爲Call和EventCustomer之間的Call Hibernate映射存在一對多關係。

+0

這個人不好,你如何寫下createCriteria(*)。createCriteria? – 2013-05-07 16:56:52

+0

它是條件實例的函數,它返回指定的Dto的另一個條件,我用它來添加限制條件。 – Ziul 2013-05-07 17:19:05