SpringBoot Jpa使用时碰到的问题总结一

1.Jpa的映射策略

在springboot使用jpa的话,会有这么一行配置:

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

这是jpa的命名策略。查阅资料后,解释是这样的:
SpringNamingStrategy 不再用作Hibernate 5.1,Hibernate 5.1已经删除了对旧的NamingStrategy 界面的支持。一个新的 SpringPhysicalNamingStrategy 现在是自动配置的,它与Hibernate的默认ImplicitNamingStrategy 结合使用 。这应该非常接近(如果不是完全相同)Spring Boot 1.3默认值,但是,您应该在升级时检查数据库模式是否正确。
如果您在升级之前已经使用Hibernate 5,则可能使用Hibernate的5默认值。如果要在升级后还原它们,请在配置中设置此属性:

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

上面的那段话的意思是说升级Hibernate到5.1的话,那么之前的hibernate.ejb.naming_strategy将不再被支持,原因是不灵活,无法正确的给定命名“规则”(the NamingStrategy contract was often not flexible enough to properly apply a given naming "rule", either because the API lacked the information to decide or because the API was honestly not well defined as it grew.)。而被替换成了下面两个属性:

hibernate.physical_naming_strategy
hibernate.implicit_naming_strategy

1.hibernate.physical_naming_strategy的解释是:

used to convert a "logical name" (either implicit or explicit) name of a table or column into a physical name (e.g. following corporate naming guidelines),翻译后:物理命名策略,用于转换“逻辑名称”(隐式或显式)的表或列成一个物理名称。这个 physical_naming_strategy 命名策略有两个子属性,如下:

//1.PhysicalNamingStrategyStandardImpl:不做修改,直接映射
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

//2.SpringPhysicalNamingStrategy:在进行领域映射时,首字母小写,大写字母变为下划线加小写
//ex: LoginName --> login_name
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

我们先来看看PhysicalNamingStrategyStandardImpl的源码:

/**
 * Standard implementation of the PhysicalNamingStrategy contract.
 * @author Steve Ebersole
 */
public class PhysicalNamingStrategyStandardImpl implements PhysicalNamingStrategy, Serializable {
    /**
     * Singleton access
     */
    public static final PhysicalNamingStrategyStandardImpl INSTANCE = new PhysicalNamingStrategyStandardImpl();

    @Override
    public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment context) {
        return name;
    }

    @Override
    public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment context) {
        return name;
    }

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        return name;
    }

    @Override
    public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment context) {
        return name;
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        return name;
    }
}

从上面的源码中我们可以发现,每个方法都是直接返回name,并没有对name做修改,所以我得出结论是:这个命名策略是指直接映射。
第二个是SpringPhysicalNamingStrategy的源码:

public class SpringPhysicalNamingStrategy implements PhysicalNamingStrategy {
    public SpringPhysicalNamingStrategy() {
    }

    public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
        return this.apply(name, jdbcEnvironment);
    }

    public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
        return this.apply(name, jdbcEnvironment);
    }

    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
        return this.apply(name, jdbcEnvironment);
    }

    public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
        return this.apply(name, jdbcEnvironment);
    }

    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
        return this.apply(name, jdbcEnvironment);
    }

    private Identifier apply(Identifier name, JdbcEnvironment jdbcEnvironment) {
        if(name == null) {
            return null;
        } else {//在大写字母前加下划线:LoginName --> Login_Name
            StringBuilder builder = new StringBuilder(name.getText().replace('.', '_'));

            for(int i = 1; i < builder.length() - 1; ++i) {
                if(this.isUnderscoreRequired(builder.charAt(i - 1), builder.charAt(i), builder.charAt(i + 1))) {
                    builder.insert(i++, '_');
                }
            }

            return this.getIdentifier(builder.toString(), name.isQuoted(), jdbcEnvironment);
        }
    }

    protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
        if(this.isCaseInsensitive(jdbcEnvironment)) {
            name = name.toLowerCase(Locale.ROOT);
        }

        return new Identifier(name, quoted);
    }

    protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) {
        return true;
    }

    private boolean isUnderscoreRequired(char before, char current, char after) {
        //2.将大写字母变为小写:Login_Name--->login_name
        return Character.isLowerCase(before) && Character.isUpperCase(current) && Character.isLowerCase(after);
    }
}

从上面的源码中我们可以发现,apply方法将大写前加下划线,再调用getIdentifier方法将大写变为小写。结合网上信息,我得出的结论是:默认认情况下,Spring Boot会配置物理命名策略。此实现提供与Hibernate 4相同的表结构:所有点都被下划线替换,骆驼情况也被下划线替换。默认情况下,所有表名都以小写生成,但如果您的架构需要,则可以覆盖该标志。(在IDEA中查看源码并断点debug可检验此结论)

2.hibernate.implicit_naming_strategy解释是:

当一个实体没有显式地命名它映射到的数据库表时,我们需要隐式地确定该表的名称。或者当特定属性没有显式地命名它映射到的数据库列时,我们需要隐式地确定该列的名称。说的简单点就是表或者属性没有指定使用名称,我的理解就是没有用@TableName和@Column。(When an entity does not explicitly name the database table that it maps to, we need to implicitly determine that table name. Or when a particular attribute does not explicitly name the database column that it maps to, we need to implicitly determine that column name.)它有以下5个属性,由于篇幅有限(我太懒-.-)。并且没有得出具体结论:

1.org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
2.org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
3.org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
4.org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
5.org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

但是我们依然可以自己修改命名策略。然后在配置文件中设置自己的命名策略。

你可能感兴趣的:(SpringBoot Jpa使用时碰到的问题总结一)