NamingStrategy in Hibernate 5, compatible with Hibernate 4

We are currently trying to port our web application from Wildfly 9.0.2 to Wildfly 10.1.0 and are therefore redirecting from Hibernate 4.3.10 to 5.0.10 .

We never defined our own NamingStrategyusing any names that Hibernate chooses for our entities (our Postgresql database is built through hbm2ddl).

Now, with Hibernate 5, we have unknown column errors, as the naming convention seems to have changed. In particular, in join tables, column names are now based on the true class instead of the parent: for example, where we have UserEntity, which inherits from AgentEntity, before we get the column agentEntity_id, now it is a userEntity_idcolumn.

I am trying to use four existing Hibernate ImplicitNamingStrategies(jpa, legacy-jpa, legacy-hbm and component path) in ours persistence.xmlwithout success: each is different from the old strategy.

So, is there a way to avoid rewriting my own strategy to keep our old model consistent?

+4
source share
1 answer

I had the same problem as switching to Hibernate 5, although we have an Oracle backend. I managed to achieve a similar naming strategy that I came across with Hibernate 4, using both org.hibernate.boot.model.naming.ImplicitNamingStrategy, and org.hibernate.boot.model.naming.PhysicalNamingStrategy.

Here's what my bean definition looks like from my application context:

@Bean
@Primary
public static JpaProperties jpaProperties() {
  final Map<String, String> hibernateConfig = newHashMap();
  hibernateConfig.put("hibernate.implicit_naming_strategy",      
  "org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl");
  hibernateConfig.put("hibernate.physical_naming_strategy",
  "com.anyapp.CustomNamingStrategy");

  final JpaProperties jpaProperties = new JpaProperties();
  jpaProperties.setProperties(hibernateConfig);
  jpaProperties.setDatabase(ORACLE);
  jpaProperties.setDatabasePlatform(ORACLE12C_DIALECT);
  jpaProperties.setGenerateDdl(false);
  jpaProperties.setShowSql(false);
  return jpaProperties;
}

, , CustomNamingStrategy , Hibernate PhysicalNamingStrategy, ImprovedNamingStrategy.

CustomNamingStrategy:

package com.anyapp;

import org.apache.commons.lang.StringUtils;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

public class CustomNamingStrategy implements PhysicalNamingStrategy {

    @Override
    public Identifier toPhysicalCatalogName(Identifier identifier, JdbcEnvironment jdbcEnv) {
        return convert(identifier);
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier identifier, JdbcEnvironment jdbcEnv) {
        return convert(identifier);
    }

    @Override
    public Identifier toPhysicalSchemaName(Identifier identifier, JdbcEnvironment jdbcEnv) {
        return convert(identifier);
    }

    @Override
    public Identifier toPhysicalSequenceName(Identifier identifier, JdbcEnvironment jdbcEnv) {
        return convert(identifier);
    }

    @Override
    public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnv) {
        return convert(identifier);
    }

    private Identifier convert(Identifier identifier) {
        if (identifier == null || StringUtils.isBlank(identifier.getText())) {
            return identifier;
        }

        String regex = "([a-z])([A-Z])";
        String replacement = "$1_$2";
        String newName = identifier.getText().replaceAll(regex, replacement).toLowerCase();
        return Identifier.toIdentifier(newName);
    }
}

, , , .

+1

Source: https://habr.com/ru/post/1674678/


All Articles