如何使用Spring定义Hibernate bean的命名策略?

时间:2016-03-24 15:34:57

标签: java database spring hibernate orm

我覆盖了所有ImprovedNamingStrategy方法并为它们设置了断点,但是在调试模式下没有调用方法。

我只有一个hibernate工厂,因为其他实例的错误是不可能的。

我认为,问题在于关键" hibernate.ejb.naming_strategy"还是不?

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
    <bean id="sqlSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.${jdbc.dialect}</prop>
                <prop key="hibernate.globally_quoted_identifiers=true">true</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">false</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">{hibernate.show_sql}</prop>
                <prop key="hibernate.ejb.naming_strategy">
                com.stub.utilities.dao.sql.hibernate.LowerCaseNamingStrategy
                </prop>
            </props>
        </property>
        <property name="annotatedClasses"/>
    </bean>
</beans>

聚甲醛

<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.core.version>5.0.7.Final</hibernate.core.version>

1 个答案:

答案 0 :(得分:4)

Hibernate 5没有任何ImprovedNamingStrategy。它使用ImplicitNamingStrategyPhysicalNamingStrategy接口。严格地说,在Hibernate 5.1中有一个类ImprovedNamingStrategy,例如。但您无法使用它来配置SessionFactory

设置ImplicitNamingStrategy

的示例
  <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="implicitNamingStrategy">
      <bean class="com.github.fluent.hibernate.cfg.strategy.hibernate5.Hibernate5NamingStrategy">
        <property name="tablePrefix" value="spring_" />
      </bean>
    </property>
  </bean>

您也可以使用hibernate.implicit_naming_strategyhibernate.physical_naming_strategy属性

<bean id="sqlSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.implicit_naming_strategy">
                  com.stub.utilities.dao.sql.hibernate.ImplicitNamingStrategy
                </prop>
                <prop key="hibernate.physical_naming_strategy">
                  com.stub.utilities.dao.sql.hibernate.PhysicalNamingStrategy
                </prop>
            </props>
        </property>
        <property name="annotatedClasses"/>
    </bean>
</beans>

您可以参考我的其他答案,了解如何实施LowerCaseNamingStrategy

Implementing a NamingStrategy in Hibernate 5 (make autogenerated column names UPPERCASE)