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.
Example for the accepted answer:
public class JBPM5NamingStrategy extends ImprovedNamingStrategy {
public String classToTableName(String className) {
retur开发者_Python百科n 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 way to rename all tables at once, is to implement your own namingStrategy (implementation of org.hibernate.cfg.NamingStrategy
).
The NamingStrategy used is specified within persistence.xml by
<property name="hibernate.ejb.naming_strategy"
value="com.example.MyNamingStrategy" />
Use a NamingStrategy. This previous answer of mine should 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:
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("_", "");
}
}
In Hibernate 5 you still have to implement this behaviour by yourself. However there are now an implicit and a physical naming strategy.
This is an exemplary implementation to prefix table names with 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.jpa.hibernate.naming.physical-strategy: my.app.PrefixPhysicalNamingStrategy
精彩评论