automatic L2 collection cache eviction when an element is added/updated/removed

Description

Hibernate should automatically evict the collection cache an entity is persisted/updated/removed.
Some precisions:

  • I'm not wanting to update the collection cache, evicting is fine

  • Doing persisted/updated/removed operation by using the collection add/remove evicts the collection

  • Doing persisted/updated/removed operation on the child entity does not evict the collection

Test case

I have test case with a Parent entity which holds a OneToMany Set<Child> called 'children':

Level 2 cache is configured as follow:

  • EhCache 1.6.2, default configuration, L2 cache activated

  • entities are not cached

  • the 'children' collection is cached

  • no queries are cached

The L2 cache works as expected when reading the Parent's children collection size:
1. new EntityManager
2. loading myParent -> makes a SQL query on the Parent table
3. calling myParent.getChildren().size() -> makes a SQL query on the Child table
4. new EntityManager
5. loading myParent -> makes a SQL query on the Parent table (since entities are not cached)
6. calling myParent.getChildren().size() -> hit the L2 cache (no SQL queries)

The test case consists in

  • persisting a new Child with a Parent

  • updating a Child's Parent (i.e. changing its Parent for another Parent)

  • deleting a Child with a Parent

When doing the test case by managing the Parent.children collection (e.g. myParent.getChildren().add(myNewChild)), the L2 cache works as expected:
the Parent.children collection is evicted from the L2 cache and the next myParent.getChildren().size() makes a SQL query.

But when doing the test case by managing the Child.parent, the L2 cache does not work as expected: the Parent.children collection is not evicted from L2 cache.
Example 1: persist

--> myParent.children has still the previous size (i.e. 0 instead of 1)

Example 2: update

-> myParent.children has still the previous size (i.e. 1 instead of 0)

Example 3: remove

-> myParent.children has still the previous size and a EntityNotFoundException is raised because the deleted cached element cannot be found in the database

This problem is also reported here:

Expected behavior

For these test cases, I expected the L2 cache behavior to be as follow:
1. when a new Child is persisted/removed, the collection which own it is evicted from the collection cache
This means:

2. when a Child parent changes, the collection which was owning the child AND the collection which own the child are evicted from the collection cache
This means:

This behavior would probably be implemented in the following classes:

Workaround:

When working with JPA/Hibernate annotations, the above behavior can be implemented using reflection as such (pseudo-code):
1. listen to persist/update/remove entity events: @EntityListeners, @PostPersist, @PostRemove, @PreUpdate
2. in the @PostPersist/@PostRemove entity event listener:
a. get all @ManyToOne fields/properties of the entity class (i.e. 'children' property of Child) and get the mapped class (i.e. Parent)
b. get the collectionName (i.e. field name of Parent class with @OneToMany annotation and a type of Collection<Child>)
c. build the collectionRole as Parent.class.getName()+"."+collectionName
d. get the entityKey as the identifier of the field found in (a)
e. call mySessionFactory.evictCollection(collectionRole, entityKey)

3. in the @PreUpdate entity event listener:
a. get all @ManyToOne fields/properties of the entity class (i.e. 'children' property of Child) and get the mapped class (i.e. Parent)
b. get the collectionName (i.e. field name of Parent class with @OneToMany annotation and a type of Collection<Child>)
c. build the collectionRole as Parent.class.getName()+"."+collectionName
d. get the entityKey as the identifier of the field found in (a) for the current entity
e. get the previousEntityKey as the identifier of the field found in (a) for the previous entity
f. if entityKey!=previousEntityKey then
call mySessionFactory.evictCollection(collectionRole, entityKey)
call mySessionFactory.evictCollection(collectionRole, previousEntityKey)

We tested this workaround with success. The event listener lasts for about 3 us/call (microseconds) which is okay.

Advantages:

  • easy for the programmer (no need for entityManager.refresh(myParent) or mySessionFactory.evictCollections())

  • clean code

Disadvantages:

  • @EntityListeners annotation to be put on every entity

  • requires Hibernate configuration by annotations on entities (does not work with XML configuration)

Environment

Hibernate 3.3.1, DB2 390

Activity

Show:
Julien Kronegg
February 23, 2010, 8:49 PM
Edited

I've added a test case to reproduce the "issue" (testCase_Hibernate_3.3.2.zip) on the following configuration:

  • Hibernate 3.3.2.GA

  • Hibernate Annotations 3.4.0.GA

  • Hibernate Commons Annotations 3.1.0.GA

  • Hibernate EntityManager 3.4.0.GA

  • Hibernate Validator 3.1.0.GA

  • EhCache 1.6.2

Julien Kronegg
February 23, 2010, 8:54 PM
Edited

I've also implemented the described workaround (workaround_HHH-4910.zip) and tested it on the same test case: it pass the test.

André Pankraz
October 31, 2012, 2:39 PM

seems still to be an issue in Hibernate 4.1.6

really...this is very very unintuitive and hard to find for joe average developers.

Andreas Berger
August 30, 2013, 9:04 AM

I added a fix for this issue to gitHub: https://github.com/hibernate/hibernate-orm/pull/580

Steve Ebersole
August 30, 2013, 3:40 PM

Thanks for the pull request Andreas, we'll take a look.

Fixed

Assignee

Brett Meyer

Reporter

Julien Kronegg

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Minor