I'm using Spring Boot 2.3.4 / Spring Data / Hibernate / MySQL 8.
My db table is called "productDef". I have an entity object "ProductDef".
When I do a "list all" query I get this error ...
java.sql.SQLSyntaxErrorException: Table 'mydatabase.product_def' doesn't exist
Why is Hibernate looking for table "product_def"?
I tried adding annotation @Table(name="productDef")
to the entity, but that didn't help.
If I rename the db table to "product_def", my code works. But unfortunately I cannot rename the db table names for my project.
What am I missing?
UPDATE:
Solution was to implement a custom PhysicalNamingStrategy to prevent the "productDef" becoming "product_def".
However, while this worked for the @Table name annotation, it did not work for the @Column name annotation.
According to this discussion thread, the ignoring of @Column name annotation is a bug.
Adding this to application.properties:
spring.jpa.properties.hibernate.physical_naming_strategy=com.myapp.dao.RealNamingStrategyImpl
And implementing the class like this:
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());
}
}