gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:57:36 25 4
gpt4 key购买 nike

我重写了所有 ImprovedNamingStrategy 方法并为它们设置了断点,但在 Debug模式下未调用这些方法。

我只有一个 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>

最佳答案

Hibernate 5 没有任何 ImprovedNamingStrategy。它使用 ImplicitNamingStrategyPhysicalNamingStrategy 接口(interface)。严格来说,在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)

关于java - 如何使用 Spring 定义 Hibernate bean 的命名策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36204108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com