I have two entities, User
and Roles
each one with active boolean parameter and ManyToMany
relationship.
This boolean parame
User
s with @OneToMany
relationship to Role
s. User-1 : Active-Role-1, Active-Role-2, In-Active-Role-3
User-2 : In-Active-Role-3
User-3 : Active-Role-1, Active-Role-2
User
records that satisfy your condition (have at-least one an active role) User-1
User-3
This is where the role of your where conditions end. Once it has been decided which record id to fetch, JPA will fetch those User
records in full. User-1
and User-3
with all their associated roles (including inactive). It might do join
or another select
but whenever it does, it will fetch all of its associated fields
In summary, Once it has decided to fetch an entity
, it cannot do any filtering on its associated fields. In this case, you expected a filtered objects in user.getRoles()
but it cannot.
If JPA had allowed that, it cannot do dirty checking
, cascading
or repeatable read
. So it does not allow it