I am currently working on an application that uses Spring Boot and Spring Data (its JpaRepository
interfaces to be precise) together with Hibernate.
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 JpaRepository
classes, this does not always seem to be the case. For that reason, I assume that there are multiple HibernateSession
instances at work here.
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 not to do it that way?
Thanks,
Alan