我有两个 JPA 实体:
@Entity
public class TaskSchedule {
...
private String name;
...
}
@Entity
public class Task {
...
private String description;
@ManyToOne
private TaskSchedule taskSchedule;
...
}
我想要一个如下所示的查询:
select t
from Task t
where t.description like '%text%' or t.taskSchedule.name like '%text%'
并非所有任务都有 TaskSchedule。上面的 JPQL 查询在生成的 SQL 中生成内连接,从而排除所有没有 TaskSchedule 的任务。
如何告诉 JPA 在生成的 SQL 中执行外连接?
我使用 Hibernate EntityManager 作为 JPA 实现。
谢谢, 戴夫
最佳答案
您可以像在 SQL 中那样通过 LEFT [OUTER] JOIN
关键字在 JPQL 中进行外部联接。 JPQL Language Reference有一个很好的例子。
徒手处理这个,完成的 JPQL 可能看起来类似于:
select t
from Task t left outer join t.taskSchedule ts
where t.description like '%text%' or ts.name like '%text%'
关于java - 如何让 JPA 在涉及关联实体的条件上使用外连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5560933/