5

Currently I'm mapping new JPA entities to an old database. The column names in the database have column names separated with underscore like 'my_column_name'.

The problem is that JPA defaults to using with camel case.

// Will be 'myColumnName' in queries and generated databases
private String myColumnName;

I know it's possible to add @Column(name="..") or @JoinColumn(name="...") on the properties - but that means i have to add it to every single property in all entities.

@Column(name = "my_column_name")
private String myColumnName;

Is it possible to change the default behavior of JPA to use 'my_column_name' instead of 'myColumnName'?

Tomas F
  • 7,226
  • 6
  • 27
  • 36

3 Answers3

3

Add the following lines to your application.properties :

spring.jpa.hibernate.naming.implicit-strategy=
   org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=
   org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

According to the following link, I tested and it worked:

Spring Boot + JPA : Column name annotation ignored

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Hubert
  • 31
  • 2
2

Unfortunately JPA doesn't provide any global naming strategy. So you should use @Column annotation on each property. But you can use Hibernate to achive this goal. See Hibernate documentation.

njjnex
  • 1,490
  • 13
  • 23
1

You need to investigate Implementing a NamingStrategy

Steve C
  • 18,876
  • 5
  • 34
  • 37