JPA multiple joins

▼魔方 西西 提交于 2020-01-14 10:15:33

问题


I have these classes

class Project {
    @ManyToOne Company owner;
    @ManyToMany Set<Person> resources;
}
class Company {
    @ManyToOne Country country;
}
class Person {
}

How can I write a JPQL to get the all the resources working on projects for companies in a specific country?

The one below doesn't seem to work (using DataNucleus)

SELECT r FROM Project p JOIN p.resources r JOIN p.owner c WHERE c.country = :country

It tries to join r with c and of course does not have the owner property and a NullPointerException is raised inside DataNucleus.


回答1:


This should do what you expect:

SELECT resource FROM Project p INNER JOIN p.resources as resource

EDIT:

I forgot a part of the initial question:

SELECT resource FROM Project p INNER JOIN p.resources as resource where p.owner.country = :country


来源:https://stackoverflow.com/questions/13741096/jpa-multiple-joins

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!