java JPA(休眠)和自定义表前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4313095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA (Hibernate) and custom table prefixes
提问by Mike Minicki
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.
是否可以覆盖 JPA/Hibernate 中的表名以便为所有项目实体添加公共前缀?例如,能够通过“JBPM5_”前缀为所有 JBPM 5 表添加前缀。
Example for the accepted answer:
已接受答案的示例:
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);
}
}
回答by Ralph
One way to rename all tables at once, is to implement your own namingStrategy (implementation of org.hibernate.cfg.NamingStrategy
).
一次重命名所有表的一种方法是实现您自己的命名策略(实现org.hibernate.cfg.NamingStrategy
)。
The NamingStrategy used is specified within persistence.xml by
使用的 NamingStrategy 在 persistence.xml 中指定
<property name="hibernate.ejb.naming_strategy"
value="com.example.MyNamingStrategy" />
回答by Sean Patrick Floyd
Use a NamingStrategy. This previous answer of mineshould provide exactly what you need.
Copied from previous answer:
从以前的答案复制:
Here is a sample NamingStrategy that builds table names of the form TYPE1_TYPE2 for join tables and adds a common prefix to all tables:
这是一个示例 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("_", "");
}
}
回答by Peter Wippermann
In Hibernate 5 you still have to implement this behaviour by yourself. However there are now an implicit and a physical naming strategy.
在 Hibernate 5 中,您仍然必须自己实现此行为。然而,现在有一个隐式和物理命名策略。
This is an exemplary implementation to prefix table names with 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);
}
}
Use this configuration property for Spring Boot 2 to activate your physical naming strategy:
使用 Spring Boot 2 的这个配置属性来激活你的物理命名策略:
spring.jpa.hibernate.naming.physical-strategy: my.app.PrefixPhysicalNamingStrategy