Many-to-Many query jpql

你说的曾经没有我的故事 提交于 2020-02-26 06:25:19

问题


I have the followed trouble.

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

@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

@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?


回答1:


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://stackoverflow.com/questions/18592533/many-to-many-query-jpql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!