Entity Class name is transformed into SQL table name with underscores
Asked Answered
T

8

87

I have the following entity defined:

@Entity
@Table(name = "EmailTemplate")
public class EmailTemplate {

Despite the table annotation, I receive java.sql.SQLException: Invalid object name 'email_template'. How can I prevent an entity class such as EmailTemplate being transformed into email_template table name?

Edit:

I'm using Spring Boot: start JPA. From my build.gradle file,

compile("org.springframework.boot:spring-boot-starter-data-jpa")
Tudor answered 16/3, 2015 at 21:52 Comment(0)
A
90

Spring by default uses org.springframework.boot.orm.jpa.SpringNamingStrategy which splits camel case names with underscore. Try setting spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy in application.properties. Check out this and this for more info.

Adcock answered 16/3, 2015 at 22:54 Comment(6)
for hibernate v5: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImplContreras
Can this be done with an annotation instead of a properties file?Hollowell
How can we achieve that in application.yml file??Amelina
@SyedZeeshanAli: It should be same like you do for server port similarly this key value pair should be definedJeaniejeanine
@SyedZeeshanAli User below code. I confirmed it is working. ` sping: jpa: hibernate: naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl `Ataractic
this did not work for meHooligan
C
83

For hibernate v5:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Contreras answered 14/2, 2017 at 18:2 Comment(0)
I
12

For Spring Boot 2 (checked with 2.2.6.RELEASE) it should be configuration yml file:

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

So you could have model like:

@Table(name = "tblDepartments")
public class Department {
    @Id
    @Column(name = "dpID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotEmpty
    @Size(min = 1, max = 25)
    @Column(name = "dpName", length = 25)
    private String name;

and populate tables at startup with data.sql:

INSERT INTO tblDepartments (dpName) VALUES ('Gryffindor');
INSERT INTO tblDepartments (dpName) VALUES ('Hufflepuff');

Also, found that for PostgreSQL additional step is needed:

@Entity
@Table(name = "\"tblEmployees\"")
public class Employee {

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

    @NotEmpty
    @Size(min = 2, max = 25)
    @Column(name = "\"empName\"", length = 25)
    private String name;

Otherwise, the names at DB will be in lowercase, like tblemployees.

Inter answered 12/5, 2020 at 19:40 Comment(0)
C
4

Use this in your appplication.properties.

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

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
Churchy answered 12/3, 2020 at 6:34 Comment(0)
A
1

Use

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
Aeromancy answered 10/11, 2015 at 20:55 Comment(0)
F
0

There are two most common org.hibernate.boot.model.naming.PhysicalNamingStrategys:

org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy
# also deprecated in 2.6 in favor of CamelCaseToUnderscoresNamingStrategy
# for removal in 2.8
org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties holds:

private void applyNamingStrategies(Map<String, Object> properties) {
    applyNamingStrategy(properties, AvailableSettings.IMPLICIT_NAMING_STRATEGY, this.implicitStrategy,
            () -> SpringImplicitNamingStrategy.class.getName());
    applyNamingStrategy(properties, AvailableSettings.PHYSICAL_NAMING_STRATEGY, this.physicalStrategy,
            () -> CamelCaseToUnderscoresNamingStrategy.class.getName());
}

so by default CamelCaseToUnderscoresNamingStrategy is in use and you have underscores...

Falgout answered 5/1, 2022 at 18:52 Comment(0)
C
-2
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a];     
nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement

Both are required :

implicit-strategy 
physical-strategy
Chap answered 12/7, 2018 at 4:35 Comment(0)
C
-3

Solved.

Invalid Object Name: Springboot with JPA(SQL server)

In application.yaml/properties specify the

spring.jpa.hibernate.naming.implicit-strategy spring.jpa.hibernate.naming.physical-strategy

jpa: show-sql: false hibernate: ddl-auto: none # Defaults to "none" when NOT in embedded mode naming: implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Chap answered 11/7, 2018 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.