使用 hql 'or' 的错误结果

Wrong result using hql 'or'

我不知道我在这里错过了什么。我正在尝试 运行 查询如下:

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();

当我分别查询 'or' 的每一侧时,我得到了正确的结果。但是当 运行 宁整个上述查询时,我只得到 'or' 左侧的结果。

有什么帮助吗?

好的,现在我对我的评论很确定了...

问题是 relatedPost.creatorPerson 将导致内部连接(实际上甚至是两个),因此当 relatedPost 时,您将不会获得 or 右侧部分的任何结果或者 relatedPost.creatorPersonnull。如果您明确指定一些左连接,您应该得到一个结果:

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