I have two entities, User
and Roles
each one with active boolean parameter and ManyToMany
relationship.
This boolean parameter is used for logical delete.
Why does the result include inactive Roles when I run this query below?
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User> query = builder.createQuery(User.class);
//Select
Root<User> user = query.from(User.class);
//Join
SetJoin<User, Role> role = user.joinSet("roles", JoinType.LEFT);
Predicate rolesActivePredicate = builder.isTrue(role.get("active"));
role.on(rolesActivePredicate);
query.multiselect(user).distinct(true);
//Where
Predicate usernamePredicate = builder.equal(user.get("username"), username);
Predicate activePredicate = builder.isTrue(user.get("active"));
query.where(usernamePredicate, activePredicate);
TypedQuery<UnikUser> typedQuery = em.createQuery(query);
return Optional.of(typedQuery.getSingleResult());
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…