dsppHibernate wrote:
1). First level cache
The sometimes so called "first level cache" is also known as the Session object, that you retrieve from the SessionFactory, sometimes delegating this to the HibernateUtil helper class.
This object is always used in Hibernate. It's the basis. You use it do anything that might have to do with the persistance of your objects.
It is also called first-level cache, because for example, if you ask for an object of the same id twice or more in a short time, it will return the already retrieved object each time, issuing only one query for that.
Obviously, as always with a cache, there're some caveats to be aware of (synchro with db, mostly), so that's why there're some patterns to follow :
The most important is that the session should be short lived and only used for a use case context. Never think using only one session for a whole app session, for example !
Session is not threadsafe, sessionfactory is. So be careful when using sessions objects in a multi-threaded environment.
dsppHibernate wrote:
2). Query cache
You should read this, it is only about twenty lines of doc :
http://www.hibernate.org/hib_docs/v3/re ... querycacheAs explained, you use it only when you've got queries that have to be executed many times. Obviously not always, because the required overhead to cache the results has to be compensated by the next call performance boost. If you cache queries that are only once or twice called, you see that you're going to get the contrary result of what you want : better perfs.
dsppHibernate wrote:
3). Second level cache
Second level is to be used for never-modified objects. So you configure this only for some classes (i.e. tables) of your system. It is designed for always accessed objects. So use second level cache only for entities that never change.
dsppHibernate wrote:
Please don't ask me to read any article. I have already read so many and now confused.
One advice : just practice, and then read the parts of the reference documentation when you need it. Don't try to read it in one shot, it's too big to do it. You won't be able to catch up everything at once.
But sorry, you're gonna have to read still a lot to be ok :-)
dsppHibernate wrote:
I have to apply caching in my project.
Well, what do you mean by this ? You have perfs problems ? If not, I do not understand why you're already thinking about caching. Optimization is always to be done after design, never before. :)