Hibernate Table Field Naming Strategies / Conventions With Spring boot.
Let’s assume that we are creating a maven spring boot application with spring data JPA.
The Spring Data JPA dependency brings in the Hibernate dependencies for us.
Also, we need database, so we are using MySQL here, so we add MySQL connector dependency / you can use any db that's not a problem.
Now we will create some Entities/Model to create respective table in database.
Suppose we have a class name Person
Set database url , username and password with some hibernate properties in application.properties.
Adding some SQL debugging in our properties file
first one is showing generated SQL and second one is to update tables data
and last one is to set dialect.
After running application, see console.
As we can see, the fields use snake case and are lowercased, following the Spring conventions.
Spring Boot Naming Strategies
Hibernate maps field names using a physical strategy and an implicit strategy.
- spring.jpa.hibernate.naming.physical-strategy defaults to org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
- spring.jpa.hibernate.naming.implicit-strategy defaults to org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
by default, they are working like this:
- Replace dots with underscores
- Change camel case to snake case, and
- Lower-case table names
example, an FirstName entity would be created as the first_name table.
So I want to change this default strategies to JPA 1.0 way. How can I do ?
We have a simple way, just add properties in application.properties file 😁.
Now drop previous db and run again application , at this time you’ll see different DDL queries.
Wait, wait … if we wanted to use the JPA 2.0 naming rules, we’d need to set ImplicitNamingStrategyJpaCompliantImpl as our implicit strategy.
I got a same result for JPA 2.0. You will also try it out.😒
Thanks for reading.
Don't forget to share 😁👍.