2017-11-26 17 views
0

남자! 내가 ManyToMany이최대 절전 모드 - ManyToMany sub는 LEFT JOIN FETCH도 선택합니다.

public class ExhibitorList { 

@ManyToMany(fetch=FetchType.EAGER) 
@JoinTable(name="exhibitor_layouts", 
    [email protected](name="exhibitor_list_id"), 
    [email protected](name="layout_id")) 
private List<Layout> layouts = new ArrayList<>(); 

} 

그리고

public class Layout { 

@ManyToMany(mappedBy="layouts",fetch=FetchType.EAGER) 
private List<ExhibitorList> exhibitor = new ArrayList<>(); 

} 

내가 엔티티를 가져 HPQ를 사용하고 관련 두 클래스 두 가지 속성이있다.

final String FIND_ALL_JOIN = "SELECT ex from ExhibitorList as ex LEFT join FETCH ex.layouts order by ex.id"; 

@Query(FIND_ALL_JOIN) 
List<ExhibitorList> findAllJoin(); 

문제는이 쿼리 나 레이아웃이 6 개 출품이 순간에 (레이아웃이있는 전시를위한 부속 선택을 얻을 수 있다는 것입니다.

Hibernate: select exhibitorl0_.id as id1_6_0_, layout2_.id as id1_8_1_, exhibitorl0_.catalogue_number as catalogu2_6_0_, exhibitorl0_.exhibitor_name as exhibito3_6_0_, exhibitorl0_.exhibitor_price as exhibito4_6_0_, exhibitorl0_.oracle_number as oracle_n5_6_0_, layout2_.created as created2_8_1_, layout2_.name as name3_8_1_, layout2_.status as status4_8_1_, layouts1_.exhibitor_list_id as exhibito1_5_0__, layouts1_.layout_id as layout_i2_5_0__ from exhibitor_list exhibitorl0_ left outer join exhibitor_layouts layouts1_ on exhibitorl0_.id=layouts1_.exhibitor_list_id left outer join layout layout2_ on layouts1_.layout_id=layout2_.id order by exhibitorl0_.id 
Hibernate: select exhibitor0_.layout_id as layout_i2_5_0_, exhibitor0_.exhibitor_list_id as exhibito1_5_0_, exhibitorl1_.id as id1_6_1_, exhibitorl1_.catalogue_number as catalogu2_6_1_, exhibitorl1_.exhibitor_name as exhibito3_6_1_, exhibitorl1_.exhibitor_price as exhibito4_6_1_, exhibitorl1_.oracle_number as oracle_n5_6_1_ from exhibitor_layouts exhibitor0_ inner join exhibitor_list exhibitorl1_ on exhibitor0_.exhibitor_list_id=exhibitorl1_.id where exhibitor0_.layout_id=? 
Hibernate: select exhibitor0_.layout_id as layout_i2_5_0_, exhibitor0_.exhibitor_list_id as exhibito1_5_0_, exhibitorl1_.id as id1_6_1_, exhibitorl1_.catalogue_number as catalogu2_6_1_, exhibitorl1_.exhibitor_name as exhibito3_6_1_, exhibitorl1_.exhibitor_price as exhibito4_6_1_, exhibitorl1_.oracle_number as oracle_n5_6_1_ from exhibitor_layouts exhibitor0_ inner join exhibitor_list exhibitorl1_ on exhibitor0_.exhibitor_list_id=exhibitorl1_.id where exhibitor0_.layout_id=? 
Hibernate: select exhibitor0_.layout_id as layout_i2_5_0_, exhibitor0_.exhibitor_list_id as exhibito1_5_0_, exhibitorl1_.id as id1_6_1_, exhibitorl1_.catalogue_number as catalogu2_6_1_, exhibitorl1_.exhibitor_name as exhibito3_6_1_, exhibitorl1_.exhibitor_price as exhibito4_6_1_, exhibitorl1_.oracle_number as oracle_n5_6_1_ from exhibitor_layouts exhibitor0_ inner join exhibitor_list exhibitorl1_ on exhibitor0_.exhibitor_list_id=exhibitorl1_.id where exhibitor0_.layout_id=? 
Hibernate: select exhibitor0_.layout_id as layout_i2_5_0_, exhibitor0_.exhibitor_list_id as exhibito1_5_0_, exhibitorl1_.id as id1_6_1_, exhibitorl1_.catalogue_number as catalogu2_6_1_, exhibitorl1_.exhibitor_name as exhibito3_6_1_, exhibitorl1_.exhibitor_price as exhibito4_6_1_, exhibitorl1_.oracle_number as oracle_n5_6_1_ from exhibitor_layouts exhibitor0_ inner join exhibitor_list exhibitorl1_ on exhibitor0_.exhibitor_list_id=exhibitorl1_.id where exhibitor0_.layout_id=? 
Hibernate: select exhibitor0_.layout_id as layout_i2_5_0_, exhibitor0_.exhibitor_list_id as exhibito1_5_0_, exhibitorl1_.id as id1_6_1_, exhibitorl1_.catalogue_number as catalogu2_6_1_, exhibitorl1_.exhibitor_name as exhibito3_6_1_, exhibitorl1_.exhibitor_price as exhibito4_6_1_, exhibitorl1_.oracle_number as oracle_n5_6_1_ from exhibitor_layouts exhibitor0_ inner join exhibitor_list exhibitorl1_ on exhibitor0_.exhibitor_list_id=exhibitorl1_.id where exhibitor0_.layout_id=? 

내가 계획을 사용하는 경우 findall은 봄 데이터() . 예를 들어, 내가 N + 1 문제

받는 방법 하나 레이아웃 모든 참가 업체 선택 얻을

답변

0

시도 :

select ex from ExhibitorList ex order by ex.id 

레이아웃과 결합 할 필요가 없습니다. 당신은 많은 관계를 가지고 있습니다.

예 사용하여 쿼리 : 당신이 where 상태 is not nullis not empty을 사용할 수 있습니다

Query q = getSession().createQuery("select ex from ExhibitorList ex order by ex.id"); 

return (List<ExhibitorList>) q.list(); 
+0

아니요 ... 이것은 내 논리를 깨뜨립니다. List 를 반환하지 않기 때문에 ... – Rostislav

+0

예가 반환합니다. 출품자 목록. 업데이트 확인. –

0

당신이 NOT NULL ListLayouts의이 즉, Layouts있는 모든 Exhibitors을 선택합니다 :

final String FIND_ALL_JOIN = "SELECT ex from ExhibitorList as ex where (ex.layouts is not null && ex.layouts is not empty) order by ex.id"; 

자세한 내용은 Expressions HQL Reference을 참조하십시오.