0
votes

Here's my scenario:

I've a process that is spaned across 2 spring transactions (P=Required,I=Default).

On T1 I instantiated an Entity A, which has an empty collection of B's.

Both, A and B entities, and the oneToMany relationship are marked with @Cache annotation.I've tried both CacheConcurrencyStrategy.READ_WRITE and CacheConcurrencyStrategy.NONSTRICT_READ_WRITE.

I'm using ehcache as the cache provider.

On T1, I merged A entity.

On T2, I merge it again.

The problem is, whenever the A entity gets merged on T2, hibernate does a query for B's collection, since he could not find it on the second level cache.

As soon as the collection gets loaded, the cache gets used (if I did the same merge on a new transaction T3,it gets the collection from the cache).

How to avoid this query to be executed? I.E: How to make hibernate second-level-cache the newly created collection, before loading it?

Also, I thought about using the first level cache, somehow managing to make one hibernate session to be thread-bounded, across T1 and T2(but I'm not sure it will work for the collection either...) Is it an acceptable procedure? how to achieve this?

TKS.

1

1 Answers

0
votes

For this issue specified by you i.e. "The problem is, whenever the A entity gets merged on T2, hibernate does a query for B's collection, since he could not find it on the second level cache." You have to check inverse property of hibernate. just setting inverse=true value, it will not fetch the B's collection again. Detail explanation of inverse property you will find at link.

i hope this will help you in understanding other issues related to inverse property.