2017-06-29 122 views
0

如何使用謂詞BooleanExpression在多對多關係上完成內部連接?如何在QueryDSL中爲多對多關係創建謂詞BooleanExpression

我有2個實體

public class A { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Integer id; 

@ManyToMany(fetch = FetchType.LAZY, 
    cascade = { CascadeType.DETACH, CascadeType.MERGE, 
     CascadeType.REFRESH, CascadeType.PERSIST}) 
    @JoinTable(name = "a_b_maps", 
     joinColumns = @JoinColumn(name = "a_id", nullable = 
      false,referencedColumnName = "id"), 
     inverseJoinColumns = @JoinColumn(name = "b_id", nullable = false, 
     referencedColumnName = "id") 
) 
    private Set<B> listOfB = new HashSet<B>(); 
} 


public class B { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Integer id; 

@ManyToMany(fetch = FetchType.LAZY, 
    cascade = { CascadeType.DETACH, CascadeType.MERGE, 
     CascadeType.REFRESH, CascadeType.PERSIST}) 
    @JoinTable(name = "a_b_maps", 
     joinColumns = @JoinColumn(name = "b_id", nullable = 
      false,referencedColumnName = "id"), 
     inverseJoinColumns = @JoinColumn(name = "a_id", nullable = false, 
     referencedColumnName = "id") 
) 
    private Set<A> listOfA = new HashSet<A>(); 
} 

一個基地回購

@NoRepositoryBean 
public interface BaseRepository<E, I extends Serializable> 
    extends JpaRepository<E, I> { 

} 

而且存儲庫類的一個

public interface Arepo extends BaseRepository<A, Integer>, 
    QueryDslPredicateExecutor<A> { 
    Page<A> findAll(Predicate predicate, Pageable pageRequest); 

} 

現在我想使用謂詞查詢回購。我需要形成一個謂語在那裏我可以加載基於一些定BS

我試圖

QA a = QA.a; 
QB b = QB.b; 
BooleanExpression boolQuery = null; 
JPQLQuery<A> query = new JPAQuery<A>(); 
    query.from(a).innerJoin(a.listOfB, b) 
    .where(b.id.in(someList)); 

現在我能夠形成JPQLQuery,但庫需要一個謂語。我怎樣才能從JPQLQuery Predicate?

或者,如何使用Predicate實現內部連接?

回答