Java 多对多查询jpql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18592533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:19:37  来源:igfitidea点击:

Many-to-Many query jpql

javajpajpa-2.0jpql

提问by Skizzo

I have the followed trouble.

我有以下麻烦。

There is an entity Distributor who is connected with the ManyToMany relationship to entity town:

有一个实体 Distributor 与实体镇的 ManyToMany 关系连接:

@Entity
public class Distributor{

   @ManyToMany
   @JoinTable( name = "GS_DISTRIBUTOR_TOWN",
           joinColumns = @JoinColumn(name = "CD_DISTRIBUTOR"),
           inverseJoinColumns = @JoinColumn(name = "CD_TOWN") )
   private List<Town> towns;

   ....
}

Then the entity town is also in relation with District

那么实体镇也与District有关

@Entity
public class Town{

   @ManyToMany(mappedBy="towns")
   private List<Distributor> distributors;

   @ManyToOne
   private District district;

   ....
}

Now i have to filter(with jpql) all distributor who are in a district. How can i do?

现在我必须过滤(使用 jpql)所有在一个地区的经销商。我能怎么做?

采纳答案by James

select distinct distributor 
from Distributor distributor  
join distributor.towns town 
join town.district district 
where district.name = :name

See: https://en.wikibooks.org/wiki/Java_Persistence/JPQL

请参阅:https: //en.wikibooks.org/wiki/Java_Persistence/JPQL