title: Spring Boot & Spring Data: how are Hibernate Sessions managed?
body: <p>I am currently working on an application that uses Spring Boot and Spring Data (its <code>JpaRepository</code> interfaces to be precise) together with Hibernate.</p>
  
  <p>One thing I love about Hiberante is its caching feature - when you submit multiple queries that match a particular object, you will get back the same instance of that object on every query execution (with respect to Java's == operator). However, when using Spring Data and <code>JpaRepository</code> classes, this does not always seem to be the case. For that reason, I assume that there are multiple <code>HibernateSession</code> instances at work here.</p>
  
  <p>My question therefore is: how does Spring Data handle Hibernate Sessions? When does it open or close them? Is there a way to configure it to use the same session for the entire runtime of my application to make full use of Hibernate's object cache? Is there a reason <em>not</em> to do it that way?</p>
  
  <p>Thanks,</p>
  
  <p>Alan</p>