Hibernate命名策略被忽略

时间:2015-07-08 03:59:08

标签: java hibernate jpa orm persistence

我通过persistence.xml在我的应用程序中配置了hibernate,如下所示:

<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>

通过创建EnityManagerFactory的EntityManagerUtil类:

            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();
        }

我尝试在persistence.xml和config overrides中为ImprovedNamingStrategy配置命名策略。但是,它不起作用,并且hibernate使用CamelCase表名生成新表。

知道我在这里做错了吗?

由于

1 个答案:

答案 0 :(得分:4)

您的映射看起来很好,所以我怀疑配置是否被引导过程中的某些内容覆盖。

尝试在此代码块的EntityManagerFactoryBuilderImpl.processProperties()方法中添加断点:

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() );
}

并查看配置是否可用。