Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA Naming Strategy - Using Camel Case

I know there are a lot of questions/answers available on this topic. Unfortunately, none of them has helped me.

I have defined an Entity.

@Entity
public class SimpleEntity ...

Now I want that the mapping to the relational database should work with same name as the one of the entity. Just plain camel case SimpleEntity. To my surprise it doesn't work. As default Spring Data JPA maps it to snake case.

I have read a lot of answers. A solution which was multiple times propagated was to set the physical naming strategy.

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

Setting this option changes the mapping naming to the lower case variant simpleentity. This might be fine when a database is used which is case insensitive but PostgreSQL isn't.

I also read in the hibernate docs that setting the implicit naming strategy should be used to define the implicit mapping (since I haven't defined any table name). But regardless which setting I'm using as implicit naming strategy the mapping remains the same. Got it, implicit naming strategy is just the logical name mapping.

I am using

spring-boot-starter-data-jpa:1.5.8.RELEASE
PostgreSQL Version 10.0

I have defined the hibernate dialect with the proper value for my database PostgreSQLDialect respective PostgreSQL95Dialect.

I know I can implement a custom PhysicalNamingStrategy. But I just want to have the same table names like my entity names. This should be the standard case, easy as pie, using plain JPA + Hibernate works fine out of the box without any configuration. So why is making Spring such a big deal?

like image 552
Chief Peter Avatar asked Dec 17 '25 06:12

Chief Peter


1 Answers

The following worked for me after struggling for a while.

  1. In your application.properties add:
spring:
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  1. Annotate SimpleEntity with the following:
@Entity
@Table(name = "\"tblEmployees\"")
public class Employee {

    @Id
    @Column(name = "\"empID\"")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

Later your insert at the console will look like this:

INSERT INTO tblEmployees (empID, ...) VALUES (22, ...)

like image 84
asolakhyan Avatar answered Dec 19 '25 21:12

asolakhyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!