0
votes

To make it clear and easy, I have two projects: 1. An Entity project where there are all the entity classes in this project. 2. An project that contains a main() function to run the application, My ehcache.xml is placed in the class path of this project.

My problem is: I can change the defaultCache element of ehcache.xml and I can see changes from the SQL log file. But I think the Hibernate only read my defaultCache element, because whatever I change in the cache element of a specific entity of ehcache.xml, there won't be any changes in the SQL log.

For example: If I set the maxElementsInMemory of defaultCache element to 0, whatever I set the maxElementsInMemory of the cache element of a entity to 100 or 0, there won't be any that entity cached in the second level cache. If I set the maxElementsInMemory of defaultCache element to 100, whatever I set the maxElementsInMemory of the cache element of a entity to 100 or 0, there are always be that entity cached in the second level cache.

So I think maybe the name attribute of the cache element of a entity is wrongly set and cannot be read from Hibernate?

2
This might be an issue with the Hibernate configuration. It would help if you could include the hibernate.cfg.xml (or hibernate.properties) file, as well as at least one Hibernate mapping file (or the Java class w/annotations). - Matt Solnit
Thank you, I found where my problem is. I'm using ehcache as cache provider. In the ehcache.xml,I think the defaultCache element is also used for ALL THE QueryCaches too, if I didn't set the standardQueryCache element. So it's not important whether or not I set the maxElementsInMemory of <cache name="business.entity.car"...> to "0", because all the QueryCache are using the defaultCache region. But setting the maxElementsInMemory of <cache name="business.entity.car"...> to "0" can definitely help to disable the sencond-level cache of that entity when using EntityManager.find() function. - Kewei Shang

2 Answers

1
votes

I found where my problem is. I'm using ehcache as cache provider. In the ehcache.xml,I think the defaultCache element is also used for ALL THE QueryCaches too, if I didn't set the standardQueryCache element. So it's not important whether or not I set the maxElementsInMemory of to "0", because all the QueryCache are using the defaultCache region. But setting the maxElementsInMemory of to "0" can definitely help to disable the sencond-level cache of that entity when using EntityManager.find() function.

0
votes

PLEASE, read the documentation. In case of maxElementsInMemory setting, a value of 0 has a special meaning - unlimited. So instead of getting no cache at all, you're caching everything.