71

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")
Jens Schauder
  • 70,783
  • 26
  • 162
  • 317
James
  • 3,484
  • 17
  • 65
  • 104

9 Answers9

78

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.

Community
  • 1
  • 1
Predrag Maric
  • 23,034
  • 4
  • 49
  • 65
67

For hibernate v5:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Corey
  • 614
  • 10
  • 17
Enginer
  • 2,736
  • 1
  • 23
  • 21
6

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');
catch23
  • 15,848
  • 39
  • 135
  • 206
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
4b0
  • 20,627
  • 30
  • 92
  • 137
nanna dash
  • 41
  • 1
1

Use

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
0

In application.properties set

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
deHaar
  • 14,698
  • 10
  • 35
  • 45
shyam_ssr
  • 1
  • 1
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...

gavenkoa
  • 41,371
  • 15
  • 229
  • 277
-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
slm
  • 14,096
  • 12
  • 98
  • 116
user8106134
  • 107
  • 1
  • 4
-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

user8106134
  • 107
  • 1
  • 4