Issue
I learning Spring boot and found that in some examples use the same properties with the prefix spring.jpa.properties
while other do it without prefix.
For instance: The article explains second level cahche href="https://www.baeldung.com/hibernate-second-level-cache" rel="nofollow noreferrer">https://www.baeldung.com/hibernate-second-level-cache and autor shows example of needed properties ( example on autor's gitHub):
hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
But it did not work for me, and I spent few hours loking for the reason, but then i noticed, someone use prefix spring.jpa.properties.
to get it working (Exact moment from video lesson):
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
Which perfectly worked for me.
So my questions are:
- why are they working in different way?
- how to understand which properties in which cases requires this prefix?
- is it probably something related to my project settings OR Spring Boot version?
- any other suggestions are appreciated :)
Thank you in advance!
Solution
There are a couple of things in play here. First the tutorial you use is using plain Spring not Spring Boot. Which means a manually configured EntityManagerFactory
on which you directly can set the provider specific properties. Like hibernate.cache.use_second_level_cache
.
As you decided to use Spring Boot you don't have a manually configured EntityManagerFactory
(although you could!). Which means you are using an autoconfigured EntityManagerFactory
. Properties for this reside in the spring.jpa
namespace for properties.
As it isn't feasible to specify properties for each and every extension for Hibernate (or other JPA providers) only some commonly used ones are exposed like spring.jpa.generate-ddl
and a few provider specific ones in the spring.jpa.hibernate
namespace. However to set all the other properties in an autoconfigured way there needed to be something else. Hence the spring.jpa.properties
prefix. Anything specified in there will be passed on as is (without the said prefix ofcourse) to the EntityManagerFactory
for configuration.
Answered By - M. Deinum
Answer Checked By - Pedro (JavaFixing Volunteer)