Search code examples
javahibernatejpaormpersistence

Hibernate Naming Strategy is ignored


I configured hibernate in my application through the persistence.xml which looks like:

<persistence-unit name="entityManager"  transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        <property name="hibernate.archive.autodetection" value="class" />   
        <property name="hibernate.hbm2ddl.auto" value="update" />       
    </properties>
</persistence-unit>

And through the EntityManagerUtil class that create EnityManagerFactory:

            Config config=CommonSettings.getInstance().config;
        Map<String, Object> configOverrides = new HashMap<String, Object>();
        configOverrides.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");
        configOverrides.put("hibernate.dialect",config.hibernateConfig.dialect);
        configOverrides.put("hibernate.show_sql", config.hibernateConfig.showSql);
        configOverrides.put("hibernate.max_fetch_depth", config.hibernateConfig.maxFetchDepth);
        configOverrides.put("hibernate.jdbc.batch_size",config.hibernateConfig.batchSize);
        configOverrides.put("hibernate.connection.pool_size", config.hibernateConfig.poolSize);
        configOverrides.put("hibernate.connection.charSet", config.hibernateConfig.charSet);
        configOverrides.put("hibernate.connection.characterEncoding",config.hibernateConfig.characterEncoding);
        configOverrides.put("hibernate.connection.useUnicode", config.hibernateConfig.useUnicode);
        configOverrides.put("hibernate.connection.autocommit", config.hibernateConfig.autocommit);
        configOverrides.put("hibernate.connection.release_mode",config.hibernateConfig.releaseMode);
        configOverrides.put("hibernate.cache.use_second_level_cache", config.hibernateConfig.useSecondLevelCache);
        configOverrides.put("hibernate.cache.use_query_cache", config.hibernateConfig.useQueryCache);
        configOverrides.put("hibernate.cache.use_structured_entries", config.hibernateConfig.useStructuredEntries);
        configOverrides.put("hibernate.cache.region.factory_class",config.hibernateConfig.factoryClass);

        configOverrides.put("packagesToScan", "org.prosolo.common.domainmodel");

        configOverrides.put("javax.persistence.jdbc.driver",config.mysqlConfig.jdbcDriver);
        configOverrides.put("javax.persistence.jdbc.url", "jdbc:mysql://"
                + host + ":" + port + "/" + database);
        configOverrides.put("javax.persistence.jdbc.user", user);
        configOverrides.put("javax.persistence.jdbc.password", password);
        try{

             emf = Persistence.createEntityManagerFactory("entityManager",
                configOverrides);

        }catch(Exception ex){
            ex.printStackTrace();
        }

I tried to configure naming strategy to ImprovedNamingStrategy in persistence.xml and in config overrides. However, it doesn't work and hibernate generates new tables with CamelCase table names.

Any idea what I'm doing wrong here?

Thanks


Solution

  • Your mapping looks just fine so I suspect the configuration is overridden by something in the bootstrap process.

    Try adding a break-point in EntityManagerFactoryBuilderImpl.processProperties() method at this code block:

    else if ( AvailableSettings.NAMING_STRATEGY.equals( keyString ) ) {
        namingStrategy = strategySelector.resolveStrategy( NamingStrategy.class, entry.getValue() );
    }
    else if ( AvailableSettings.NAMING_STRATEGY_DELEGATOR.equals( keyString ) ) {
        namingStrategyDelegator = strategySelector.resolveStrategy( NamingStrategyDelegator.class, entry.getValue() );
    }
    

    and see if the configuration is available there.