Join elimination in 7.x skips @SQLRestriction when querying by to-one association id

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?

No, that change is not intentional. Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.

Thanks for confirming ! I’ve put together a reproducer based on the test case
template: example sql restriction breaks on one-to · vbrown37/hibernate-test-case-templates@2dfba2f · GitHub

The test filters children by parent id and asserts that the generated SQL
contains no join on the parent table, so the @SQLRestriction is skipped
(children of a deleted parent are returned).

I could not create an issue due to lack of permissions.

I’m also linking this PR Honor SQL restrictions for optimized to-one association paths by vbrown37 · Pull Request #13239 · hibernate/hibernate-orm · GitHub.

Please take a look at the following similar reports about not being able to create Jira tickets:

We heard a couple of times from users that it takes a bit of time until newly created accounts are fully activated, so maybe try again the next day.

I was able to create a ticket, thank you.

Thanks. Please always post the link to the Jira ticket for cross referencing, so other people who run into this topic can follow up on the matter.

HHH-20806