Hello,
@SQLRestriction no longer applied when filtering by associated entity id after Hibernate 6 → 7 upgrade
Versions
- From: Hibernate ORM
6.6.53.Final - To: Hibernate ORM
7.2.19.Final
Description
After upgrading, a query filtering by a @ManyToOne association id no longer applies the @SQLRestriction declared on the associated entity.
Minimal example
@Entity
@SQLRestriction("deleted = false")
class ParentEntity {
@Id
UUID id;
boolean deleted;
}
@Entity
class ChildEntity {
@Id
UUID id;
@ManyToOne(fetch = FetchType.LAZY)
ParentEntity parent;
}
interface ChildRepository extends CrudRepository<ChildEntity, UUID> {
List<ChildEntity> findByParentId(UUID parentId);
}
Hibernate 6 behavior
Hibernate generated a join and applied the restriction:
select c.*
from child_entity c
left join parent_entity p
on p.id = c.parent_id
and p.deleted = false
where p.id = ?
Children of a deleted parent were not returned.
Hibernate 7 behavior
Hibernate now generates:
select c.*
from child_entity c
where c.parent_id = ?
The associated entity is not joined, so @SQLRestriction("deleted = false") is not applied, and children of a deleted parent are returned.
Question
Is this change intentional in Hibernate 7?
If yes, what is the recommended way to preserve the previous behavior without adding explicit joins to every query filtering by an associated entity id?