本文介绍了JPA(休眠)和自定义表前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JPA / Hibernate中重写表名以便为所有项目实体添加通用前缀?例如,能够以JBPM5_前缀为所有JBPM 5表前缀。

Is it possible to override table names in JPA/Hibernate in order to add a common prefix for all project entities? For instance to be able to prefix all JBPM 5 tables by "JBPM5_" prefix.

接受答案的示例:

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


推荐答案

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

One way to rename all tables at once, is to implement your own namingStrategy (implementation of org.hibernate.cfg.NamingStrategy).

所使用的NamingStrategy在persistence.xml中由

The NamingStrategy used is specified within persistence.xml by

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

这篇关于JPA(休眠)和自定义表前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 05:04