Hibernate naming strategy changing table names

前端 未结 5 1946
别那么骄傲
别那么骄傲 2020-12-05 12:01

I\'m a little bit confused by hibernates (version 5.1) naming strategy - namely it changes my table name and I\'d like to avoid that. Also - spring.jpa.hibernate.namin

5条回答
  •  长情又很酷
    2020-12-05 12:12

    The problem lies in spring-boot-1.4 - it seems like they have changed the properties (or whatever) I've now found this answer ImprovedNamingStrategy no longer working in Hibernate 5, but it still didn't resolve correctly. So I have changed the code a little to not use the underscore method and to extend the newly introduced class SpringPhysicalNamingStrategy:

    package com.foo;
    
    import org.hibernate.boot.model.naming.Identifier;
    import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
    import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
    
    import java.io.Serializable;
    import java.util.Locale;
    
    
    public class RealNamingStrategyImpl extends org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implements Serializable {
    
        public static final PhysicalNamingStrategyImpl INSTANCE = new PhysicalNamingStrategyImpl();
    
        @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 in application.properties I've changed the deprecated line to

    spring.jpa.properties.hibernate.physical_naming_strategy=.RealNamingStrategyImpl
    

    Now it uses exactly the table and column names as I have them in my entity files.

提交回复
热议问题