【问题标题】:JPA (Hibernate) and custom table prefixesJPA (Hibernate) 和自定义表前缀
【发布时间】:2011-05-17 19:39:52
【问题描述】:

是否可以覆盖 JPA/Hibernate 中的表名以便为所有项目实体添加公共前缀?例如,能够为所有 JBPM 5 表加上“JBPM5_”前缀。

已接受答案的示例:

public class JBPM5NamingStrategy extends ImprovedNamingStrategy {
   public String classToTableName(String className) {
      return StringHelper.unqualify(className);
   }
   public String propertyToColumnName(String propertyName) {
      return propertyName;
   }
   public String tableName(String tableName) {
      return "JBPM5_" + tableName;
   }
   public String columnName(String columnName) {
      return columnName;
   }
   public String propertyToTableName(String className, String propertyName) {
      return "JBPM5_" + classToTableName(className) + '_'
         + propertyToColumnName(propertyName);
   }
}

【问题讨论】:

  • 你的意思是除了@Table注解之外?
  • 是的,除了 Table 注释。我实际上是指一个 3rd 方项目,所以只有配置是可行的(我无法触摸编译的 java 源)。
  • 而且在单个类上使用注释来实施站点范围的策略既乏味又容易出错

标签: java hibernate jpa


【解决方案1】:

在 Hibernate 5 中,您仍然需要自己实现此行为。然而,现在有一种隐含的和物理的命名策略。

这是用 Hibernate 5 为表名添加前缀的示例性实现:

package my.app;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

public class PrefixPhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {

    /**
     * TODO Make this an injectable application property
     */
    public static final String TABLE_NAME_PREFIX = "MY_PREFIX_";

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        Identifier newIdentifier = new Identifier(TABLE_NAME_PREFIX + name.getText(), name.isQuoted());
        return super.toPhysicalTableName(newIdentifier, context);
    }
}

使用 Spring Boot 2 的这个配置属性来激活你的物理命名策略:

spring.jpa.hibernate.naming.physical-strategy: my.app.PrefixPhysicalNamingStrategy
 

【讨论】:

    【解决方案2】:

    使用NamingStrategy。这个previous answer of mine 应该能提供你所需要的。

    从上一个答案复制:

    这是一个示例 NamingStrategy 建立表格的表名 TYPE1_TYPE2 用于连接表并添加一个 所有表的通用前缀:

    public class CustomNamingStrategy extends ImprovedNamingStrategy {
    
        private static final long serialVersionUID = 1L;
        private static final String PREFIX = "PFX_";
    
        @Override
        public String classToTableName(final String className) {
            return this.addPrefix(super.classToTableName(className));
        }
    
        @Override
        public String collectionTableName(final String ownerEntity,
                final String ownerEntityTable, final String associatedEntity,
                final String associatedEntityTable, final String propertyName) {
            return this.addPrefix(super.collectionTableName(ownerEntity,
                    ownerEntityTable, associatedEntity, associatedEntityTable,
                    propertyName));
        }
    
        @Override
        public String logicalCollectionTableName(final String tableName,
                final String ownerEntityTable, final String associatedEntityTable,
                final String propertyName) {
            return this.addPrefix(super.logicalCollectionTableName(tableName,
                    ownerEntityTable, associatedEntityTable, propertyName));
        }
    
        private String addPrefix(final String composedTableName) {
    
            return PREFIX
                    + composedTableName.toUpperCase().replace("_", "");
    
        }
    
    }
    

    【讨论】:

    • +1,从来不知道 NamingStrategy,你每天都会学到新东西!
    • 这是一个休眠特定的东西。它不适用于其他 JPA 提供程序
    • 感谢您的努力和如此冗长的回答,肖恩。但实际上要归功于 Ralph,以获取有关如何通过持久性配置注入策略的信息。因此,对我来说这是一个更完整的答案。
    • @Michał 当然,给 Ralph 打勾,但我也在我链接到的上一个答案中显示了配置。
    • 不要误会,肖恩。我认为您的回复在内容方面要好得多。谢谢,对我帮助很大。但拉尔夫的回答更充分地解决了这个问题。
    【解决方案3】:

    一次重命名所有表的一种方法是实现自己的命名策略(org.hibernate.cfg.NamingStrategy 的实现)。

    使用的 NamingStrategy 在 persistence.xml 中指定

    <property name="hibernate.ejb.naming_strategy"
              value="com.example.MyNamingStrategy" />
    

    【讨论】:

    • 有人可以编辑并粘贴一个示例到这个答案吗?
    猜你喜欢
    • 2014-12-23
    • 1970-01-01
    • 2015-01-24
    • 2016-06-15
    • 1970-01-01
    • 2013-09-06
    • 2021-03-31
    • 1970-01-01
    • 2021-04-23
    相关资源
    最近更新 更多