左连接不是反复空值
当A.B.name喜欢"%pac%"时,我需要获得所有记录A.或A.C.name喜欢"%pac%"
Class<T> entityClass = (Class<T>) A.class;
PathBuilder<T> entity = new PathBuilder<T>(entityClass, "entity");
EntityManager entityManager = entityManagerProvider
.getEntityManager(entity.getType());
JPAQuery query = new JPAQuery(entityManager);
query = query.from(entity);
PathBuilder<?> associationPathB = entity.get("B");
query = query.leftJoin(associationPathB).fetch();
PathBuilder<?> associationPathC = entity.get("C");
query = query.leftJoin(associationPathC).fetch();
BooleanBuilder baseSearch = new BooleanBuilder();
baseSearch.and(associationPathB.getString("name").lower().like("%pac%"))
.or(associationPathC.getString("name").lower().like("%pac%"));
query.where(baseSearch.getValue());
long count = query.count();
生成的查询是
select entity from A entity
left join fetch entity.B
left join fetch entity.C
where lower(entity.B.name) like ?1 escape '!'
or lower(entity.C.name) like ?2 escape '!'
count只返回2条记录,但我有10条A类记录,其中B.name喜欢=&#39; pac&#39;。
C列中只有2条记录不为空,但这是OR条件
的连接为什么不退回10条记录?
如果我在DB中执行查询,则返回10个寄存器
select * from A entA
left join B entB on entB.id = entA.entB
left join C entC on entC.id = entA.entC
where entB.name like '%pac%' OR
entC.name like '%pac%'