如何为QueryDSL中的多对多关系创建谓词BooleanExpression [英] How to create Predicate BooleanExpression for many to many relations in QueryDSL
问题描述
如何使用谓词BooleanExpression对多对多关系进行内部联接?
How can inner join be done on Many to Many relations using Predicate 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> {
}
以及A的存储库类
public interface Arepo extends BaseRepository<A, Integer>,
QueryDslPredicateExecutor<A> {
Page<A> findAll(Predicate predicate, Pageable pageRequest);
}
现在,我想将A Repo与Predicate查询一起使用.我需要形成一个谓词,在其中我可以根据一些给定的B加载A
Now I want to use A Repo with Predicate query. I need to form a predicate where I can load A based on some given 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获取谓词?
Now I am able to form a JPQLQuery
, but the repository expects a Predicate. How can I get Predicate from the JPQLQuery??
或者,如何使用谓词实现内部联接?
Or, how can the inner join be achieved using Predicate?
推荐答案
借助此处给出的答案,我能够创建谓词
I am able to create a Predicate with the help of answer given here
https://stackoverflow.com/a/23092294/1969412 .
SO,我直接使用
a.listOfB.any()
.id.in(list);
这就像一种魅力.
这篇关于如何为QueryDSL中的多对多关系创建谓词BooleanExpression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!