Kamil Kamil - 3 months ago 117
Java Question

Spring Boot + JPA : Column name annotation ignored

I have a Spring Boot application with dependency "spring-boot-starter-data-jpa". My entity class has a column annotation with column name. For example:

@Column(name="TestName")
private String testName;


SQL generated by this created test_name as column name. After looking for solution I have found that
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
solved the problem (column name is taken from column annotation).

Still, my question is why without naming_strategy set to
EJB3NamingStrategy
JPA is ignoring column annotation? Maybe hibernate dialect has something to do with it? I'm connecting to MS SQL 2014 Express and my logs contains:

Unknown Microsoft SQL Server major version [12] using SQL Server 2000 dialect
Using dialect: org.hibernate.dialect.SQLServerDialect

Answer

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")
Comments