问题
Just like in hibernate, in EclipseLink we have the annotation @AdditionalCriteria that allow us to add a filter on our data. In hibernate it as @Filter and you can either add it on top a a class or on a field like this.
@Filter(name="test", condition=":deleted is null")
public class MyClass { ... }
or
@Filter(name="test", condition=":deleted is null")
private List<MyClass> list;
In EclipseLink the @AdditionalCriteria only works for the first on, on a class. Is there any other annotation that works like the second one, on a field?
Thanks
回答1:
I don't recommend this functionality as it changes the view of the entity from what is in the database, but the functionality still exists in EclipseLink - it just isn't exposed directly in an annotation.
Instead, you will need to use a customizer to modify the mapping - changing the mapping so that it includes the filter expression you need. This is described here
Be aware though that changes to the referenced entity that might affect the filter will not be reflected in the cache. Any changes to MyClass instances that might cause them to fail the condition, do not automatically cause them to be removed from the Entity's list - you must handle this yourself, or force a refresh of the entity directly when the transaction completes.
来源:https://stackoverflow.com/questions/41811494/additionalcriteria-on-a-variable-rather-than-a-class-in-eclipselink