13

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) {
      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);
   }
}
Mike Minicki
  • 8,216
  • 11
  • 39
  • 43

3 Answers3

26

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" />
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Ralph
  • 118,862
  • 56
  • 287
  • 383
23

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("_", "");

    }

}
Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • +1, Never knew about NamingStrategy, you learn something new every day! – Qwerky Nov 30 '10 at 11:38
  • 4
    It's a hibernate specific thing. It doesn't work with other JPA providers – Sean Patrick Floyd Nov 30 '10 at 11:39
  • Thanks for the effort and so verbose answer, Sean. But the credits actually go to Ralph for the info on how to inject the strategy via persistence config. And as such is a more complete answer for my eyes. – Mike Minicki Nov 30 '10 at 12:02
  • @Michał sure, give Ralph the checkmark, but I have also shown the config in the previous answer I linked to. – Sean Patrick Floyd Nov 30 '10 at 12:07
  • Don't get me wrong, Sean. I think your reply is far better when it comes to content. Thank you, it helped me a lot. But Ralph's answer addresses the question more adequately. – Mike Minicki Nov 30 '10 at 13:50
  • You are taking this very seriously, aren't you? :-) – Sean Patrick Floyd Nov 30 '10 at 14:12
  • Well, you're never sure who's on the other side, right? First and foremost, I don't want to hurt feelings when I don't have to; and second, yes, I like to treat seriously everything I do :) – Mike Minicki Nov 30 '10 at 17:23
  • this is very nobel, and I am not surprised to see you are from Poland. I have come to know Poles as one of the People with the highest passion for honor and justice. But while I respect that a lot, my own Austrian / American / almost German heritage makes me a little bit more laid back and a bit more cynical, too. Which is why it's great to have people like you around :-) – Sean Patrick Floyd Nov 30 '10 at 22:17
14

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
 
Peter Wippermann
  • 4,125
  • 5
  • 35
  • 48