有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringBootHibernate5忽略@Table和@Column

这让我快发疯了

我正在实现SpringSocial,它要求您拥有一个名为UserConnection的数据库表(而不是使用标准命名约定,即使用下划线分隔两个单词)

因此,在我天真的世界观中,我认为通过指定@Table(name="UserConnection")可以很容易地解决这个问题。。。但是不,那太容易了

注释被忽略,表被创建为user_connection,这将导致SpringSocial产生hissy拟合

请告诉我,有一种简单的方法可以告诉我的Spring Boot应用程序只命名一个表(及其对应的列),使用驼峰大小写命名约定而不是标准的命名约定


共 (2) 个答案

  1. # 1 楼答案

    选项1

    首先,在@Entity映射中定义表名:

    @Entity( name = "UserConnections")
    public class UserConnection{
    

    选项2

    你应该用NamingStrategy支付一点费用。为sessionFactory bean定义属性时,请尝试添加以下内容:

    <prop key="hibernate.implicit_naming_strategy">legacy-jpa</prop>
    

    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.

    因此,如果不想为每个实体显式命名表名,则应遵循此策略

    选项3

    或者,如果上述方法不适用于您,您必须使用PhysicalNaming策略。虽然这是你最后的选择:

    参考:https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/domain/naming.html

  2. # 2 楼答案

    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
    

    详细答案

    正如Spring Boot 1.4 release notes所述:

    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中添加以下内容来切换到该命名策略:

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

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

    SpringPhysicalNamingStrategy^{}方法是理解这种行为的关键:

    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);
    }
    

    它基本上用下划线替换任何.和大小写更改(看看^{}方法)