简体   繁体   English

多对多查询jpql

[英]Many-to-Many query jpql

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.现在我必须过滤(使用 jpql)所有在一个地区的经销商。 How can i do?我能怎么做?

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

First, from the entity Town, there is incorrect relationship mapping for @Manytoone.首先,从实体 Town 来看,@Manytoone 的关系映射不正确。 Should be:应该:

@Entity
public class District {

  .....

   @ManyToOne
   private Town town;

   ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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