JPA query with @ManyToMany relationship and no navigation

时光毁灭记忆、已成空白 提交于 2019-12-13 00:09:49

问题


Similar to this post, I have these (almost the same) classes:

public class Project {
    @ManyToMany 
    private Set<Person> resources;
    // get and set of resources
}
public class Person {
}

The difference is that my properties are private (using beans as entities).

The question is: how would I create a query to return all projects of a determined person (in JPQL and/or using CriteriaQuery)?

I found all these other similar questions, but none helped me, because all of them rely on the navigation from Project to Person (which doesn't exist querying from Person):

  • JPQL ManyToMany select
  • @ManyToMany JPA 2 complex query
  • JPA 2.0 CriteriaQuery on tables in @ManyToMany relationship

I wouldn't like to insert a property inside 'Person' just to be able to make the query, because it doesn't make sense in my model.

Thanks!!


回答1:


select project from Project project
join project.resources person
where person.id = :personId

I'll let you translate this to criteria if you really want it, but I don't hink using Criteria for such a basic static query offers any advantage.



来源:https://stackoverflow.com/questions/22052537/jpa-query-with-manytomany-relationship-and-no-navigation

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