Martin Hlavňa (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiODNkOGIzZTA1...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-15688?atlOrigin=eyJpIjoiODNkOG...
) HHH-15688 (
https://hibernate.atlassian.net/browse/HHH-15688?atlOrigin=eyJpIjoiODNkOG...
) Entity with lazy loaded association ignores filters (
https://hibernate.atlassian.net/browse/HHH-15688?atlOrigin=eyJpIjoiODNkOG...
)
Issue Type: Bug Affects Versions: 6.1.3 Assignee: Unassigned Created: 08/Nov/2022 02:10 AM
Environment: Wildfly 27.Beta.1 , Java 17
Probably also affects 6.1.4 and 6.1.5 but we are using Hibernate provided in platform.
Priority: Major Reporter: Martin Hlavňa (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
I am implementing soft delete with Hibernate. Each entity that supports soft-deleting has
an attribute for it.
@Column(name = "DELETED")
private boolean deleted;
I have created @FilterDef in package-info.java for package with domain objects.
@FilterDef(name = "deletedFilter",
parameters = @ParamDef(name = "includeDeleted", type = Boolean.class),
defaultCondition = ":includeDeleted = true OR DELETED = false"
)
applied it to all DeleteAware entities
@Filter(name = "deletedFilter")
public class CustomerGroup
and enabled in when using in queries
Session session = em.unwrap(Session.class);
session.enableFilter("deletedFilter")
.setParameter("includeDeleted", fp.isDeleted());
Filter is applied and works correctly for primary entity (for example when I query
customers I can see that additional where condition is always applied as needed).
Problem is with filter of association. Let's say Customer entity has collection of
CustomerGroup.
@ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
@JoinTable(name = "CUSTOMER_CUSTOMER_GROUP",
joinColumns = @JoinColumn(name = "CUSTOMER_ID"),
inverseJoinColumns = @JoinColumn(name = "CUSTOMER_GROUP_ID"))
private Set<CustomerGroup> groups;
However when I query for Customer, groups collection contains deleted entities. I have
turned on sql logging and I can see that condition is not applied for lazy query. However
if I change
@ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
to
@ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
it works.
Both entities are annotated with @Filter. I have also tried applying @Filter annotation to
collection itself without success. For initial testing I have also ensured that filters
are not disabled and includeDeleted parameter is always false.
@Where annotation on entities works like a charm, but cannot be disabled (99% of queries
we want to filter out deleted objects but there is that pesky 1% where we need deleted
ones).
Looks like filters are not applied when relation is lazy loaded.
(
https://hibernate.atlassian.net/browse/HHH-15688#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-15688#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100210- sha1:ffe1a02 )