4

Sorry for the long details, but I wanted to be clear and thorough ...

I'm using Sprint Boot 2.3.4 with spring-boot-starter-data-jpa and spring-boot-starter-web in the pom.xml with MySQL 8 on Mac.

DB Table Name: `productDef`
DB Columns: `id` | `productName`

My entity looked like this ...

@Entity
public class ProductDef {
    private Integer id;
    private String productName;
    /* ... */
}

Hibernate complained that it couldn't find table "product_def".

(My understanding is) this is because the default physical naming strategy determines the table name by converting class name "ProductDef" to "product_def".

So I added a @Table annotation like this, but that did not fix the problem.

@Entity
@Table(name="productDef")
public class ProductDef {
    private Integer id;
    private String productName;
    /* ... */
}

To make the annotation work, I also had to implement a custom physical naming strategy like so ...

package com.myapp.dao;

import java.io.Serializable;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

public class RealNamingStrategyImpl extends org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implements Serializable {

    public static final PhysicalNamingStrategy INSTANCE = new PhysicalNamingStrategyStandardImpl();

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        return new Identifier(name.getText(), name.isQuoted());
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        return new Identifier(name.getText(), name.isQuoted());
    }

}

And add it to the application.properties file like so:

spring.jpa.properties.hibernate.physical_naming_strategy=com.myapp.dao.RealNamingStrategyImpl

Then the @Table(name="productDef") annotation works.

However --

Hibernate now complains that it cannot find column "product_name" (for the object property "productName").

I added annotation @Column(name="productName") but it did not work. The @Column annotation appears to be ignored.

I found a similar question about this on Stackoverflow here ... Spring Boot + JPA : Column name annotation ignored

I tried the solution from there, adding these to the application.properties ...

First tried this ...

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

... then tried this ...

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

But neither worked.

@Column(name="productName") continues to be ignored.

QUESTION: How do I get @Column(name="productName") to not be ignored?

(Also note, I found a discussion thread here that leads me to believe this could be a bug in Spring Boot?)

AvaTaylor
  • 623
  • 4
  • 16
  • 1
    how are you getting the table created? using sql or setting ddl-auto to true? Could you share the ddl if you are using any? – balias Oct 09 '20 at 04:19
  • I'm creating the table with a SQL script ... looks like this ... CREATE TABLE `productDef` ( `id` int NOT NULL AUTO_INCREMENT, `productName` varchar(100) NOT NULL DEFAULT, PRIMARY KEY (`id`) ) ENGINE=InnoDB CHARSET=utf8 COLLATE=utf8_general_ci; – AvaTaylor Oct 13 '20 at 02:20
  • @AvaTaylor, did you find a solution for that ? – Adir Dayan Aug 24 '21 at 17:33

0 Answers0