简体   繁体   中英

JPA query with @ManyToMany relationship and no navigation

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 ):

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!!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM