多对多查询jpql

Ski*_*zzo 17 java jpa jpql jpa-2.0

我遇到了麻烦.

有一个实体分销商与实体城镇的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;

   ....
}
Run Code Online (Sandbox Code Playgroud)

然后实体城镇也与区有关

@Entity
public class Town{

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

   @ManyToOne
   private District district;

   ....
}
Run Code Online (Sandbox Code Playgroud)

现在我必须过滤(使用jpql)所有分区中的经销商.我能怎么做?

Jam*_*mes 35

select distinct distributor 
from Distributor distributor  
join distributor.towns town 
join town.district district 
where district.name = :name
Run Code Online (Sandbox Code Playgroud)

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