@Where annotation does no refresh after updating the child collection


I have an entity using the @Where annotation as the following code

@OneToMany(fetch=FetchType.LAZY, mappedBy = "otherthing")
@Where(clause="FLAG_A='N' AND FLAG_B='N'")
private List<OtherThing> listOtherThing;

It works when I load entity from database. In a transaction I modify this list, changing the @Where condition clauses to the ‘Y’ value

OtherThingRepository repo;
OtherThing ex=repo.findById(123);

and when I reload the main entity (still in the transaction)

MainObjectRepository repo;
MainObject ex=repo.findById(456);
ex.getListOtherThings(); // <--- HERE

I can see this list with updated values, while these new values should be excluded by the annotation.

If I get the entity after this transaction, the annotation correctly works and I cannot see the list with flags different from ‘N’ value.

It seems like the annotation only works when there is the first load of the entity, but updates aren’t managed during transaction. Any ideas?


How do you reload it? Unless you use refresh, the entity is served from the cache, so no query is issued.


I load from cache, I don’t refresh it and I cannot refresh it because I need the entity from the transaction. I think anyway the annotation can be confusing, because I think the Where condition is always true when retrieving data from repository (even if Hibernate read it from cache)


The @Where annotation Javadoc and the User Guide clearly indicates that the clause attribute takes an SQL fragment which is passed to the SQL query.

Hence, no dynamic filtering of in-memory entities for the @Where annotation.


I don’t know @Filter annotation but reading the doc it can be useful, the only drawback is I have to enable the filter calling it by name and currently I have a lot of occurrences of Where annotation.

Anyway thank you for the suggestion :wink:


You’re welcome. I don’t know if the @Filter annotation will help you here since it also translates to an SQL filtering on the SQL clause. But if you load an entity, you are responsible for making sure the in-memory changes are in sync prior to flushing them to the DB.

This is similar to making sure that bidirectional associations are in sync.