Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA (Hibernate) and custom table prefixes

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);
   }
}
like image 212
Mike Minicki Avatar asked Nov 30 '10 11:11

Mike Minicki


People also ask

Is @column mandatory in JPA?

Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

Can JPA create tables?

For this reason, the ability to have JPA frameworks -- such as EclipseLink or Hibernate -- create tables and databases as they bootstrap is built right into the specification. These database creation facilities are also a great way to validate a newly set up Hibernate and JPA development environment.

What is GenerationType in hibernate?

SEQUENCE is the generation type recommended by the Hibernate documentation. The generated values are unique per sequence. If we don't specify a sequence name, Hibernate will reuse the same hibernate_sequence for different types.

What is PhysicalNamingStrategy?

The Hibernate PhysicalNamingStrategy methods allow you to customize the default naming conventions for the following database identifiers: toPhysicalCatalogName – customize the default database catalog naming convention. toPhysicalSchemaName – customize the default database schema naming convention.


2 Answers

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" />
like image 182
Ralph Avatar answered Sep 21 '22 08:09

Ralph


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

    }

}
like image 21
Sean Patrick Floyd Avatar answered Sep 20 '22 08:09

Sean Patrick Floyd