使用hql'或'错误的结果

时间:2015-08-13 12:11:51

标签: hibernate hql

我不知道我在这里失踪了什么。我尝试按以下方式运行查询:

String query = "select e from Event e where (eventType.operation='LIKE' and relatedPost.creatorPerson.personId=:person1Id)"
                + " or (eventEntity.relatedEntity='PERSON' and eventType.operation='FOLLOW' and relatedPerson.personId=:personId)"
                + " order by creationDate desc";
beginTx();
Query q = session.createQuery(query);
q.setInteger("personId", id1);
q.setInteger("personId1", id2);
List<Event> events = q.list();
commitTx();

当我查询&#39;或&#39;的每一面时另外,我得到了正确的结果。但是当运行整个上面的查询时,我只得到&#39;或&#39;左侧的结果。

有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

好的,现在我对我的评论非常肯定......

问题在于,relatedPost.creatorPerson会导致内部联接(实际上甚至是两次),因此orrelatedPost时,relatedPost.creatorPerson的正确部分不会得到任何结果{1}}是null。如果明确指定了一些左连接,则应该得到结果:

select e from Event e 
left join e.relatedPost as rp 
left join rp.creatorPerson as cp
where (eventType.operation='LIKE' and cp.personId=:person1Id) 
or (eventEntity.relatedEntity='PERSON' and eventType.operation='FOLLOW' and relatedPerson.personId=:personId) order by creationDate desc