JPA查询与@ManyToMany关系,没有导航 [英] JPA query with @ManyToMany relationship and no navigation

查看:266
本文介绍了JPA查询与@ManyToMany关系,没有导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章类似,我有这些(几乎相同)的类:

  public class Project {
@ManyToMany
private Set< Person>资源;
//获取和设置资源

public class Person {
}

不同之处在于我的属性是私有的(使用bean作为实体)。



问题是:如何创建一个查询来返回所有这些确定人员的项目(在JPQL和/或使用CriteriaQuery)?我发现所有这些其他类似的问题,但没有人帮助我,因为他们都依赖于从 Project Person (它不存在从 Person ):



我不想在'Person'中插入一个属性只是为了查询,因为它在我的模型中没有意义。



感谢!!

解决方案

从项目项目中选择项目
加入project.resources人员
其中person.id =:personId

如果您确实需要它,我会让您将其转换为标准,但我不会使用Criteria为这样的基本静态查询提供任何优势。


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.

这篇关于JPA查询与@ManyToMany关系,没有导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆