Hibernate加入

时间:2013-05-07 14:16:15

标签: hibernate join

如何在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");

并且在某些情况下我收到此错误

2 个答案:

答案 0 :(得分: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映射存在一对多的关系。

答案 1 :(得分: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");