Filtering non-primitive types with hibernate
-
27-06-2021 - |
Question
In my database I do not delete objects. Instead a soft delete is done. Means a delted flag is set to true. This should be filtered by hibernate using the @Filter annotation. This works but I have the problem that fields are not filtered.
For example I have a child class and a parent class which contains a child. If the child gets deleted and the parent gets loaded the parent object contains the child object (although the deleted flag is set correctly). But I want the child object to be filtered like it works with collections. Is there any way to do that?
I tried treating the field like a collecting but this didn't work for me.
@Entity
@FilterDef(name = deletedFilter, parameters = @ParamDef(name = deletedParam, type = "boolean"))
@Filter(name = deletedFilter, condition = ":deletedParam = deleted")
public class Child {}
@Entity
public class Parent {
@OneToOne
@Filter(name = deletedFilter, condition = ":deletedParam = deleted")
private Child child = new Child();
}
La solution
Hibernate filters are not working on @OneToOne
relations.
https://hibernate.onjira.com/browse/HHH-4026
The explanation is on the comments to the issue: filters would change the cardinality of the relation.
Autres conseils
If the boolean flag doesn't work you can use Integer value instead. And can check accordingly.