title: Caching with Hibernate + Spring - some Questions
body: <p>I'm working on developing a web application with Spring 3 and Hibernate 3.6. At the moment I try to understand how Caching with Spring and Hibernate works. I found some sources about Caching with Hibernate and some about Spring and I try to bring my information together now. I still got some questions to both frameworks and I'd be glad if someone could answer them or tell me if the facts listed here are correct.</p>
  
  <p>Most of the time, short answers (yes/no) would be sufficient. I think that this list can be useful for others too, who want to understand how caching with spring and hibernate works.</p>
  
  <p><code>General</code></p>
  
  <p><strong>1)</strong> Hibernate supports the following Caches: 1st Level Cache, 2nd Level Cache, Query Cache</p>
  
  <p><strong>2)</strong> Spring itself supports the following Caching possibilities: just Method Caching </p>
  
  <p><code>1st Level Cache</code></p>
  
  <p><strong>3)</strong> The 1st Level Cache is part of EVERY Hibernate application. </p>
  
  <p><strong>4)</strong> The 1st Level Cache is created for EVERY hibernate-session. </p>
  
  <p><strong>5)</strong> What is saved in the 1st Level Cache? Objects or just the values of their properties? queries and their results?</p>
  
  <p><code>2nd Level Cache</code></p>
  
  <p><strong>6)</strong> I found out: the 2nd Level Cache is used ONCE per application. isn't that false? isn't it used ONCE per <code>SessionFactory</code>? and: multiple sessionfactorys = multiple 2nd level caches possible?</p>
  
  <p><strong>7)</strong> what is saved in the 2nd Level Cache: in my opinion just the values belonging to one record, not the objects itself.</p>
  
  <p><strong>8)</strong> when storing values from one record in 2nd Level Cache, its possible to store related values (from objects connected over a foreign key) with it too?</p>
  
  <p><strong>9)</strong> when updating the values of one object in the 2nd level cache, it's possible to update the values of objects connected with it in the cache too?</p>
  
  <p><strong>10)</strong> when values of an object are changing, how can I update the 2nd level cache? flush? can I just update a part of the cache or must the whole cache be updated?</p>
  
  <p><strong>11)</strong> where does the 2nd level cache make sense and where doesn't it?</p>
  
  <p><strong>12)</strong> the Cache Mode: does each cache mode provide a different strategy of caching? for example with cache mode "read-only" no synchronization of database and cache is ever necessary? do other cache modes provide synchronization? I thought synchronization must be done by the developer himself?</p>
  
  <p><code>Query Cache</code></p>
  
  <p><strong>13)</strong> what is the difference between the Query Cache and the 2nd Level Cache? in my opinion: in the Query Cache result sets are saved, but not with their values, just with their ids. when the query is used again and the result set is still "correct", the values belonging to the ids are queried from the 2nd Level Cache</p>
  
  <p><strong>14)</strong> For the Query Cache a 2nd Level Cache MUST be used?</p>
  
  <p><strong>15)</strong> where does the Query Cache make sense and where doesn't it?</p>
  
  <p><code>Spring</code></p>
  
  <p><strong>16)</strong> Does Spring provide more Caching possibilities than method caching?</p>
  
  <p><strong>17)</strong> method caching is not linked to hibernate caching</p>
  
  <p><strong>18)</strong> but: for method caching the 2nd level is necessary, like Ehcache (which can be used by hibernate too)</p>
  
  <p><strong>19)</strong> can method caching be used without database queries?</p>
  
  <p><code>Getting mixed up</code></p>
  
  <p><strong>20)</strong> if using ehcache for hibernate as 2nd level cache and Ehcache for spring for method caching, can I use the same Ehcache-instance? is there a chance that something gets mixed up?</p>
  
  <p><strong>21)</strong> when using 1st level cache and 2nd level cache, can they get mixed up? when querying the database, where does the result then come from, the 1st or 2nd level cache? does the 1st level cache work with the 2nd level cache?</p>
  
  <p><strong>22)</strong> anything else that can get mixed up by using the caches I mentioned? :-)</p>
  
  <p>Thanks for answering, no matter what question! :-)</p>