【问题标题】:JPA use both hbm.xml and annotationsJPA 同时使用 hbm.xml 和注解
【发布时间】:2018-10-19 07:01:17
【问题描述】:

我正在将我的技术堆栈从 Spring 3 升级到 Spring 4,并将 Hibernate 3 升级到 HIbernate 4。 目前我们正在使用 Hibernate3Support 和模板,现在我们正在转向实体管理器。 所有 db 条目都在 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>

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

【问题讨论】:

    标签: spring hibernate entitymanager


    【解决方案1】:

    在代码中发现问题,该行应该是:

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

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2021-07-03
      相关资源
      最近更新 更多