31

我有以下麻烦。

有一个实体 Distributor 与实体城镇的多对多关系相关联:

@Entity
public class Distributor{

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

   ....
}

那么实体镇也和District有关系

@Entity
public class Town{

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

   @ManyToOne
   private District district;

   ....
}

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

4

2 回答 2

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

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

于 2013-09-03T14:03:52.923 回答
-2

首先,从实体 Town 来看,@Manytoone 的关系映射不正确。应该:

@Entity
public class District {

  .....

   @ManyToOne
   private Town town;

   ....
}
于 2020-08-12T19:08:10.013 回答