JavaLearner
JavaLearner

Reputation: 647

Difference between spring.jpa.properties.hibernate and spring.jpa.hibernate

I am working on a Spring Boot project and using Spring Data JPA with Hibernate as JPA implementation.

Currently in my application.yml file I have the following properties:

spring:
    jpa:
        show-sql: true
        properties:
            hibernate:
                format_sql: true
                generate_statistics: true
        hibernate:
            ddl-auto: none
            dialect: org.hibernate.dialect.H2Dialect

There are Hibernate properties with different prefixes(spring.jpa.properties.hibernate and spring.jpa.hibernate)

What is the purpose of having these difference and can they be used interchangeably, meaning can I replace spring.jpa.properties.hibernate.format_sql with spring.jpa.hibernate.format_sql?

Upvotes: 18

Views: 20102

Answers (2)

1615903
1615903

Reputation: 34879

This is explained in the Spring Boot Reference Documentation at Configure JPA Properties:

-- all properties in spring.jpa.properties.* are passed through as normal JPA properties (with the prefix stripped) when the local EntityManagerFactory is created.

So, spring.jpa.hibernate.X properties are used by Spring, and spring.jpa.properties are passed on to whatever JPA implementation you are using, allowing you to set configuration properties that Spring does not have.

Upvotes: 18

Vy Do
Vy Do

Reputation: 52746

For simplification, there are no more than convention, exactly. Just care about value of key.

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format-sql=true
spring.jpa.properties.hibernate.generate_statistics=true
spring.jpa.hibernate.ddl-auto=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.H2Dialect

or

spring.jpa.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format-sql=true
spring.jpa.properties.hibernate.generate_statistics=true

are the same.

For more information, you can see https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-configure-jpa-properties

https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-configure-hibernate-naming-strategy just for easy for remember something.

In addition, all properties in spring.jpa.properties.* are passed through as normal JPA properties (with the prefix stripped) when the local EntityManagerFactory is created.

(source: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-configure-jpa-properties)

Upvotes: -3

Related Questions