改进的命名策略Hibernate 4无法正常工作

时间:2016-05-21 06:02:20

标签: java hibernate jpa

我一直在更新应用程序中的库并添加新功能 我从Hibernate 3迁移到4,我不得不对xml进行一些调整,ImprovedNamingStrategy停止工作。

我确定下面的配置存在问题:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/data/jpa
                            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">


  <!--  How to include more then one base package -->

  <context:annotation-config/>
  <context:component-scan base-package="com.lotjm"/>
  <context:property-placeholder location="classpath:./properties/database.properties"/>

  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="jpaDialect">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
  </bean>

  <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="com.lotjm"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="generateDdl" value="true"/>
        <property name="showSql" value="true" />
      </bean>
    </property>
    <property name="jpaProperties">
      <props>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

      </props>
    </property>

    <property name="persistenceProvider">
      <bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
    </property>

  </bean>

  <jpa:repositories base-package="com.lotjm.repository"
                    factory-class="org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean"/>

  <tx:annotation-driven transaction-manager="transactionManager"/>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">validate</prop>
      </props>
    </property>

    <property name="annotatedClasses">
      <list>
        <value>com.xx.domain.User</value>
        <value>com.x.domain.Application</value>
        <value>com.xx.domain.Project</value>
        <value>com.xxx.domain.Document</value>
        <value>com.xxx.domain.AbstractAuditingEntity</value>
      </list>
    </property>
    <property name="namingStrategy">
      <bean class="org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy"/>
    </property>
  </bean>

  <bean id="auditingProvider" class="com.lotjm.config.audit.UsernameAuditorAware"/>

  <bean id="dateTimeService" class="com.lotjm.config.audit.CurrentTimeDateTimeService"/>

  <bean id="dateTimeProvider" class="com.lotjm.config.audit.AuditingDateTimeProvider">
    <constructor-arg index="0" ref="dateTimeService"/>
  </bean>

  <jpa:auditing auditor-aware-ref="auditingProvider" set-dates="true" date-time-provider-ref="dateTimeProvider"/>

</beans>

虽然数据库上有一个名为document_history的列,但应用程序正在创建一个名为documentHistory的新列

即使是创建新列的事实也是错误的。

<prop key="hibernate.hbm2ddl.auto">validate</prop>

1 个答案:

答案 0 :(得分:1)

解决问题的两件事 属性名称错误,必须是jpaPropertyMap 和hibernate.ejb.naming_strategy错过了ejb

<property name="**jpaPropertyMap**">
  <props>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">validate</prop>
    <prop key="hibernate.**ejb**.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
  </props>
</property>