title: Add prefix to table names in liquibase using Java Spring JPA
body: <p>I'm searching for a way to automatically add a prefix to my table names when liquibase executes my xml files. For example, if this is my initial-changelog.xml:</p>
  <pre><code> &lt;changeSet id=&quot;initial_changeset&quot; author=&quot;ME&quot;&gt;
      &lt;createTable tableName=&quot;EMP&quot;&gt;
          &lt;column name=&quot;ID&quot; type=&quot;UUID&quot;&gt;
              &lt;constraints primaryKey=&quot;true&quot; nullable=&quot;false&quot;/&gt;
          &lt;/column&gt;
          &lt;column name=&quot;REFERENCE&quot; type=&quot;VARCHAR(250)&quot;&gt;
              &lt;constraints nullable=&quot;false&quot;/&gt;
          &lt;/column&gt;
      &lt;/createTable&gt;
  
      &lt;createIndex tableName=&quot;EMP&quot; indexName=&quot;IDX_EMP_REFERENCE&quot; unique=&quot;true&quot;&gt;
          &lt;column name=&quot;ESS_REFERENCE&quot;/&gt;
      &lt;/createIndex&gt;
  
      &lt;createTable tableName=&quot;COMPANY_CAR&quot;&gt;
          &lt;column name=&quot;ID&quot; type=&quot;UUID&quot;&gt;
              &lt;constraints primaryKey=&quot;true&quot; nullable=&quot;false&quot;/&gt;
          &lt;/column&gt;
          &lt;column name=&quot;START_DATE&quot; type=&quot;DATE&quot;&gt;
              &lt;constraints nullable=&quot;false&quot;/&gt;
          &lt;/column&gt;
          &lt;column name=&quot;END_DATE&quot; type=&quot;DATE&quot;&gt;
              &lt;constraints nullable=&quot;false&quot;/&gt;
          &lt;/column&gt;
      &lt;/createTable&gt;
  &lt;/changeSet&gt;
  </code></pre>
  <p>I would like to have my tables generated with a specific prefix, for example:</p>
  <ul>
  <li><code>TBL_EMP</code></li>
  <li><code>TBL_COMPANY_CAR</code></li>
  </ul>
  <p>Can I specify a prefix in the XML? Or is there an other way to force it?</p>
  <p>Also the tableName indexes should have the prefixes too...</p>
  <p>I'm using liquibase with a MYSQL database using Java Spring JPA</p>
  <p><strong>TRY 1:</strong> I have tried this already but this doesn't overwrite the liquibase xml config:</p>
  <pre><code>hibernate.physical_naming_strategy=be.everesst.employee.adapter.jpa.config.CustomHibernateNamingStrategy
  </code></pre>
  <p>With this in my <code>application.properties</code> file</p>
  <pre><code>import org.hibernate.boot.model.naming.Identifier;
  import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
  import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
  import org.springframework.context.annotation.Configuration;
  
      @Configuration
      public class CustomHibernateNamingStrategy extends PhysicalNamingStrategy {
      
      public static final String TABLE_NAME_PREFIX = &quot;EMPLOYEE_&quot;;
      
      @Override
      public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnvironment) {
          return prefix(identifier);
      }
  
      private Identifier prefix(Identifier identifier) {
          return Identifier.toIdentifier(TABLE_NAME_PREFIX + identifier.getText());
      }
      
   }
  </code></pre>
  <p><strong>TRY 2:</strong> I have tried this alsobut this doesn't overwrite the liquibase xml config aswell:</p>
  <pre><code>spring.jpa.hibernate.naming.physical-strategy=be.everesst.employee.adapter.jpa.config.PrefixPhysicalNamingStrategy 
  </code></pre>
  <p>With this in my <code>application.properties</code> file</p>
  <pre><code>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 {
  
      public static final String TABLE_NAME_PREFIX = &quot;EMPLOYEE_&quot;;
  
      @Override
      public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
          Identifier newIdentifier = new Identifier(TABLE_NAME_PREFIX + name.getText(), name.isQuoted());
          return super.toPhysicalTableName(newIdentifier, context);
      }
  </code></pre>
  <p><strong>FOUND A SOLUTION of Liquibase itself: using the modifySql statement</strong></p>
  <p>(I removed all the custom hibernate naming strategies!)</p>
  <p>I created 2 properties:</p>
  <pre><code>&lt;property name=&quot;table.prefix&quot; value=&quot;TBL_&quot;/&gt;
  &lt;property name=&quot;schema.name&quot; value=&quot;PUBLIC&quot;/&gt;
  </code></pre>
  <p>Then added the following statements:</p>
  <pre><code>    &lt;modifySql&gt;
          &lt;regExpReplace replace=&quot;CREATE\ TABLE\ ${schema.name}.([\w]*)\ (.*)&quot; with=&quot;CREATE TABLE ${schema.name}.${table.prefix}$1 $2&quot;/&gt;
      &lt;/modifySql&gt;
  
      &lt;modifySql&gt;
          &lt;regExpReplace replace=&quot;CREATE\ UNIQUE\ INDEX\ ${schema.name}.([\w]*)\ ON\ PUBLIC.([\w]*)\((.*)\)&quot; with=&quot;CREATE UNIQUE INDEX ${schema.name}.$1 ON ${schema.name}.${table.prefix}$2($3)&quot;/&gt;
      &lt;/modifySql&gt;
  
      &lt;modifySql&gt;
          &lt;regExpReplace replace=&quot;CREATE\ INDEX\ ${schema.name}.([\w]*)\ ON\ ${schema.name}.([\w]*)\((.*)\)&quot; with=&quot;CREATE INDEX ${schema.name}.$1 ON ${schema.name}.${table.prefix}$2($3)&quot;/&gt;
      &lt;/modifySql&gt;
  </code></pre>