代码之家  ›  专栏  ›  技术社区  ›  Mike Minicki

JPA(Hibernate)和自定义表前缀

  •  11
  • Mike Minicki  · 技术社区  · 14 年前

    是否可以覆盖jpa/hibernate中的表名,以便为所有项目实体添加一个通用前缀?例如,能够用“jbpm5_uu”前缀给所有jbpm 5表加前缀。

    接受答案示例:

    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);
       }
    }
    
    3 回复  |  直到 6 年前
        1
  •  25
  •   Sean Patrick Floyd    14 年前

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

    所使用的NamingStrategy在persistence.xml中由

    <property name="hibernate.ejb.naming_strategy"
              value="com.example.MyNamingStrategy" />
    
        2
  •  21
  •   Community Neeleshkumar S    7 年前

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

    从上一个答案复制:

    这是一个例子 生成窗体的表名 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("_", "");
    
        }
    
    }
    
        3
  •  6
  •   Peter Wippermann    6 年前

    在Hibernate5中,您仍然需要自己实现这个行为。然而,现在有了隐式和物理命名策略。

    这是一个用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);
        }
    }
    

    使用SpringBoot2的这个配置属性来激活物理命名策略:

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