0

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 Answers1

0

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.

Community
  • 1
  • 1
kamlesh patel
  • 375
  • 2
  • 13
  • 1
    Hi kamlesh, thanks for the answer. I'm using JPA annotations, and my A-B oneToMany relationship is already using "mappedby" (which I expect to have the same effect of inverse) property, i.e A is the weak side. However, the scenario I told persists. I got it working by making the B collection, which was instantiated on the field declaration null instead of empty. Seems like hibernate distinguish between empties and null collections afterall – Luiz Henrique Martins Lins Rol Oct 24 '12 at 11:18
  • I forgot to mention that the query was executed even for empty collections. Although my solution of nullifying the collections just works for the newly created entities, it solved my business case for now,(80% of the time it was quering on empty collections) and I stopped researching further. Of course I'd still want a 100% solution. – Luiz Henrique Martins Lins Rol Oct 24 '12 at 11:23