Spring Boot + JPA : Column name annotation ignored
For Hibernate 5, I solved this issue by adding the following lines in my application.properties file:
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
The default strategy for @Column(name="TestName")
will be test_name
, this is correct behavior!
If you have a column named TestName
in your database you should change Column annotation to @Column(name="testname")
.
This works because database does not care if you name your column TestName or testname (column names are case insensitive!!).
But beware, the same does not apply for database name and table names, that are case sensitive on Unix systems but case in sensitive on Windows systems (the fact that probably kept a lot of people awake at night, working on windows but deploying on linux :))
It seems that
@Column(name="..")
is completely ignored unless there is
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
specified, so to me this is a bug.
I spent a few hours trying to figure out why @Column(name="..") was ignored.
By default Spring uses org.springframework.boot.orm.jpa.SpringNamingStrategy
to generate table names. This is a very thin extension of org.hibernate.cfg.ImprovedNamingStrategy
. The tableName
method in that class is passed a source String
value but it is unaware if it comes from a @Column.name
attribute or if it has been implicitly generated from the field name.
The ImprovedNamingStrategy
will convert CamelCase
to SNAKE_CASE
where as the EJB3NamingStrategy
just uses the table name unchanged.
If you don't want to change the naming strategy you could always just specify your column name in lowercase:
@Column(name="testname")