I have 2 entities as described below. What I would like to achieve is to fetch the related entity in a single query, but only the ones with a specific status. This can be achieved by adding and identity1_.status_id=1
to the JOIN
of the SQL query.
I found that additional conditions with ON
or WITH
are supported for a JOIN
but not for a JOIN FETCH
. Instead, I get the message with-clause not allowed on fetched associations; use filters
. So I tried filters and found it to be working for one-to-one and one-to-many associations but not for many-to-one. Besides, using filters in this case feels very cumbersome instead of just being able to extend the query with the desired conditions.
Christian Bauer stated many years ago (Hibernate Community • View topic - Filters on Many-To-One association):
Filters should (not work at all) on a many-to-one, because that is conceptually impossible (the only thing a filter can do is return nothing, which violates the cardinality of the association, it would turn into a many-to-zero-or-one).
First of all, I don’t understand what is so wrong about this concept? Moreover, I don’t see any other way of getting my use case to work.
So, any ideas on how to solve my issue?
Entities
@Entity
public class Role {
@Id
@Column(name = "id", unique = true)
private Integer id;
@ManyToOne
@JoinColumn(name = "owner_identity_id")
private Identity owner;
}
@Entity
public class Identity {
@Id
@Column(name = "id", unique = true)
private Integer id;
@Column(name = "status_id")
private Integer statusId;
@OneToMany(mappedBy = "owner")
private List<Role> ownedRoles;
}
JPQL
SELECT role
FROM Role AS role
LEFT JOIN FETCH role.owner
SQL (current)
select
role0_.id as id1_32_0_,
identity1_.id as id1_24_1_,
role0_.owner_identity_id as owner_id4_32_0_,
identity1_.status_id as status_id53_24_1_
from
role role0_
left outer join
identity identity1_
on role0_.owner_identity_id=identity1_.identity_id
SQL (required)
select
role0_.id as id1_32_0_,
identity1_.id as id1_24_1_,
role0_.owner_identity_id as owner_id4_32_0_,
identity1_.status_id as status_id53_24_1_
from
role role0_
left outer join
identity identity1_
on role0_.owner_identity_id=identity1_.identity_id
and identity1_.status_id=1