开发者

How can get JPA to use an outer-join on a condition involving an associated entity?

I have two JPA entities:

@Entity
public class TaskSchedule {
    ...
    private String name;
    ...
}开发者_JAVA百科

@Entity
public class Task {
    ...
    private String description;

    @ManyToOne
    private TaskSchedule taskSchedule;
    ...
}

I would like to have a query that looks like this:

select t
from Task t
where t.description like '%text%' or t.taskSchedule.name like '%text%'

Not all Tasks have a TaskSchedule. The above JPQL query generates an inner-join in the resulting SQL, which excludes all of the tasks without a TaskSchedule.

How can I tell JPA to perform an outer-join in the generated SQL?

I am using Hibernate EntityManager as the JPA implementation.

Thanks, Dave


You can have an outer join in JPQL like you do in SQL, through the LEFT [OUTER] JOIN keywords. The JPQL Language Reference has a pretty good example of this.

Freehanding this, the finished JPQL will probably look similar to:

select t
from Task t left outer join t.taskSchedule ts
where t.description like '%text%' or ts.name like '%text%'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜