Hibernate命名策略被忽略

我通过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表名生成新表。

知道我在这里做错了吗?

谢谢

采纳答案:

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

尝试在此代码块的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() );
}

并查看配置是否可用。

author: vlad-mihalcea

参考更多解答: Hibernate Naming Strategy is ignored ,转载请保留出处Hibernate命名策略被忽略及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:orm