前提:
已知A表,B表,且 A表一对多B表
查询B表数据的同时,同步关联的A表数据
1. A表的实体类中:
@OneToMany(mappedBy = "b", fetch = FetchType.LAZY) @JsonIgnore private Set<B> bs = new HashSet<>();
2. B表的实体类中:
@QueryInit("*.*.*.*") @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "关联的字段,例如a_id") private A a;
3. 在B表的repository interface中,添加EntityGraph
@Override @EntityGraph(attributePaths = {"a"}) Page<B> findAll(Predicate predicate, Pageable pageable);
备注:如果在查询过程中,使用了BooleanBuilder,QXXX q = QXXX.xxx;动态构建查询条件,则注意在Repository中,extends QuerydslPredicateExecutor
public interface xxRepository extends JpaRepository<C, String> { } public interface xxRepository extends JpaRepository<B, String>, QuerydslPredicateExecutor<B> {
标签:LAZY,private,查询,EntityGraph,备忘录,extends,使用,interface,qudsl From: https://www.cnblogs.com/uoky/p/18426956