问题
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