I am trying to find out if enabling the second level cache provided by hibernate will help my performance and after reading through the documentation I am still a little confused and i have a couple of questions.
1. Do entities get stored in the second level cache even when you are asking for an entity by something other than its primary key?
I search for Field's by name or name and category based on different criteria. So I cannot make name and category a composite primary key because category can sometimes be null. Lets say I do the following multiple times
The name and category are the same for a lot of the queries, so I really do not want to go out to the db every time I perform this query. If I turn on the second level cache will I avoid going to the db every time I make this query?
Will this work with native queries? Or does it only work when you query using EJBQL or HQL?
In my example below I need to make sure that each field is unique based on name and category (ie there is only one row in the db per name and category). To do this I issue a native query with a "SELECT FOR UPDATE" (I want a pessimistic lock, i.e. I want to block other threads until the lock is released). Will I still benefit from the second level cache?
It won't work with a native query unless the results of the native query is used to create JPA entities, that is, create and initialize JPA POJOs, AND then those created POJOs are associated with the Hibernate Session at some point. I highly doubt that is happening, so a highly probable answer would be 'no.'
As a quick aside I am trying to turn on the cache in jboss 4.2.3 and have some problems.
In my persistence.xml:
But when I deploy I get:
Looking at jboss it seems like the sever already has a cache configuration file in the deploy directory named "ejb3-entity-cache-service.xml". Should I use that one? Do i need to provide a treecache.xml file with my EAR?
This might be worth starting a new topic. When a thread is a few days old, with multiple replies, fewer people are likely to view it. A new topic with a descriptive subject heading might catch the attention of someone who's dealt with that specific issue.