Hibernate 6 @Filter behaves differently compared to Hibernate 5

448 Views Asked by At

I have a question. It seems that the @Filter annotation is behaving differently between hibernate 5.6. 14.Final and Hibernate 6.1.5.Final

I have 2 entities both have a @Filter(name = FILTER_NOTDELETED)

They are mapped like this

Entity 1 :
@Table("entity1")
@Filters({
@Filter(name = "notDeleted", condition = "DELETED=0")
})
public class Entity1 {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "StaffID")
private Staff staff;
}
Entity 2:

@Table(name = "Staff")
@Filters({
@Filter(name = "notDeleted", condition = "DELETED=0")
})
public class Staff {
...
}

Thanks

When running the following test case:


var entity = getSession().createQuery("from Entity1 where id = :id", Entity1.class)
.setParameter("id", 18874846)
.uniqueResult();

        assertNotNull(entity.getStaff());
        assertThat(entity.getStaff().getDeleted()).isNotZero();

The getSession() method simply activates the notDeleted filter


public Session getSession() {
  Session session = getCurrentSession();
  session.enableFilter("notDeleted");
  return session;
}

with Hibernate 5 I get the following queries:

Hibernate: /* from Entity1 where id = :id */ select ... from Entity1 prescripti0_ where prescripti0_.deleted=0 and prescripti0_.Entity1ID=?
Hibernate: select ... from Staff staff0_ where staff0_.StaffID=?

but on Hibernate 6 I get the deleted=0 also for the staff query:

Hibernate: /* from Entity1 where id = :id */ select ... from Entity1 p1_0 where p1_0.deleted=0 and p1_0.mandantId=? and p1_0.Entity1ID=?
Hibernate: select ... from Staff s1_0 where s1_0.StaffID=? and s1_0.DELETED=0

What has changed between the 2 hibernate version (I know that a LOT) How can I achieve the same behaviour as in 5.6 ?

1

There are 1 best solutions below

0
Bogdan Marinescu On

Turns out this is a bug and will be fixed with https://hibernate.atlassian.net/browse/HHH-16179

Thanks to the Hibernate team for their support :)