Spring Boot Hibernate 5忽略@Table和@Column [英] Spring Boot Hibernate 5 Ignoring @Table and @Column

查看:1151
本文介绍了Spring Boot Hibernate 5忽略@Table和@Column的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我很生气。

我正在实施Spring Social,它要求你有一个名为 UserConnection (而不是使用下划线分隔两个单词的标准命名约定。)

I'm implementing Spring Social and it requires you to have a database table named UserConnection (instead of using the standard naming convention of using an underscore to separate the two words).

所以在我天真的世界观中,我认为它会是通过指定 @Table(name =UserConnection)轻松解决...但不,这太简单了。

So in my naive world view, I assumed it would be easily solved by specifying @Table(name="UserConnection")... but no, that would be all too easy.

注释被忽略,表格被创建为 user_connection ,这会导致Spring Social产生异议。

The annotation is ignored and the table is created as user_connection which then causes Spring Social to have a hissy fit.

请告诉我有一些简单的方法告诉我的Spring Boot应用程序只是命名一个表(及其相应的列)使用驼峰式命名约定而不是标准的约定。

Please tell me there's some easy way to tell my Spring Boot app to just name that one table (and its corresponding columns) to use a camel-case naming convention instead of the standard one.

推荐答案

TL; DR



将以下内容添加到 application.yml 文件中:

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

或你的 application.properties

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



详细答案



As Spring Boot 1.4 发布说明状态:


不再使用SpringNamingStrategy ,因为Hibernate 5.1已删除
支持旧的 NamingStrategy 界面。新的
SpringPhysicalNamingStrategy 现在已自动配置,在
组合中使用Hibernate的默认 ImplicitNamingStrategy 。这个
应该与Spring Boot 1.3
默认值非常接近(如果不相同),但是,升级时应检查数据库模式是否正确

SpringNamingStrategy is no longer used as Hibernate 5.1 has removed support for the old NamingStrategy interface. A new SpringPhysicalNamingStrategy is now auto-configured which is used in combination with Hibernate’s default ImplicitNamingStrategy. This should be very close to (if not identical) to Spring Boot 1.3 defaults, however, you should check your Database schema is correct when upgrading.

这个新的 PhysicalNamingStrategy 遵循Spring推荐的命名约定。无论如何,如果你想要完全控制物理命名,你最好使用 org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 。您可以通过将以下内容添加到 application.yml 来切换到该命名策略:

This new PhysicalNamingStrategy follows Spring recommended naming conventions. Anyway if you want total control over physical naming, you're better off using the org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. You can switch to that naming strategy by adding the following to your application.yml:

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




注释被忽略,表格被创建为 user_connection
然后导致Spring Social出现异议。

The annotation is ignored and the table is created as user_connection which then causes Spring Social to have a hissy fit.

应用 方法 SpringPhysicalNamingStrategy 是了解此行为的关键:

The apply method of SpringPhysicalNamingStrategy is the key to understand this behavior:

private Identifier apply(Identifier name, JdbcEnvironment jdbcEnvironment) {
    if (name == null) {
        return null;
    }
    StringBuilder builder = new StringBuilder(name.getText().replace('.', '_'));
    for (int i = 1; i < builder.length() - 1; i++) {
        if (isUnderscoreRequired(builder.charAt(i - 1), builder.charAt(i),
                builder.charAt(i + 1))) {
            builder.insert(i++, '_');
        }
    }
    return getIdentifier(builder.toString(), name.isQuoted(), jdbcEnvironment);
}

private boolean isUnderscoreRequired(char before, char current, char after) {
    return Character.isLowerCase(before) && Character.isUpperCase(current)
            && Character.isLowerCase(after);
}

它基本上取代任何和案例更改(看看 isUnderscoreRequired method)带下划线。

It basically replaces any . and case changes (take a look at isUnderscoreRequired method) with an underscore.

这篇关于Spring Boot Hibernate 5忽略@Table和@Column的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆