Tomas F
Tomas F

Reputation: 7596

How to change the default format of JPA generated column names

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'?

Upvotes: 5

Views: 6548

Answers (3)

Hubert
Hubert

Reputation: 31

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

Upvotes: 3

njjnex
njjnex

Reputation: 1555

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.

Upvotes: 2

Steve C
Steve C

Reputation: 19445

You need to investigate Implementing a NamingStrategy

Upvotes: 1

Related Questions