JPA同时使用hbm.xml和注释

时间:2018-10-19 07:01:18

标签: spring hibernate entitymanager

我正在将技术堆栈从Spring 3升级到Spring 4,将Hibernate 3升级到HIbernate 4。 当前,我们正在使用Hibernate3Support和模板,现在我们转向实体管理器。 所有的数据库条目都在hbm.xml中,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.mycompany.Employee" table="employee">
    <id column="employeeid" name="employeeID">
      <generator class="assigned"/>
    </id>
    <property name="name" type="string">
      <column length="100" name="name"/>
    </property>
    <many-to-one class="com.mycompany.Department" name="department"/>
   </class>
</hibernate-mapping>

配置如下:

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="myDataSource"/>
        </property>
        <property name="mappingLocations">
            <value>classpath*:com/**/*.hbm.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">com.mycompany.ExMySQLInnoDBDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

现在,我们希望将系统与要在注释中创建的新实体一起使用,并且我们需要支持hbm.xml文件,因为我们有很多文件并且无法手动转换它们。 新配置:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="packagesToScan" value="com.mycompany"/>
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="persistenceUnitPostProcessors">
        <list>
            <bean
class="org.springframework.data.jpa.support.ClasspathScanningPersistenceUnitPostProcessor">
                <constructor-arg index="0" value="com.mycompany" />
                <property name="mappingFileNamePattern" value="classpath*:com/**/*hbm.xml" />
            </bean>
        </list>
    </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

当我运行应用程序然后获取该实体的映射异常时,请让我知道该怎么做。

1 个答案:

答案 0 :(得分:0)

得到代码中的问题,该行应为:

<property name="mappingFileNamePattern" value="**/*hbm.xml" />