Hibernate和JPA错误:在依赖Maven项目上重复导入

Ste*_*ane 3 dependencies jpa maven maven-module

我有两个Maven项目,一个叫做项目数据,另一个叫项目-rest,它依赖于项目数据项目.

Maven构建在项目数据项目中是成功的,但在项目休息项目中失败,例外情况如下:

Caused by: org.hibernate.DuplicateMappingException: duplicate import: TemplatePageTag refers to both com.thalasoft.learnintouch.data.jpa.domain.TemplatePageTag and com.thalasoft.learnintouch.data.dao.domain.TemplatePageTag (try using auto-import="false")
Run Code Online (Sandbox Code Playgroud)

我可以在这里看到一些解释:http://isolaso​​ftware.it/2011/10/14/hibernate-and-jpa-error-duplicate-import-try-using-auto-importfalse/

我不明白的是,为什么在构建项目数据项目时不会发生此消息,而在构建项目休息项目时会发生此消息.

我试着查看pom.xml文件,看看是否有什么可以解释这个问题.

我还查看了测试配置的方式并在项目休息项目上运行.

但我还没有看到任何东西.

编辑:添加Maven项目: www.learnintouch.com/learnintouch-data.tar.gz www.learnintouch.com/learnintouch-rest.tar.gz

tma*_*wen 11

该错误基本上是由于sessionFactorybean是两个具有相同逻辑名称TemplatePageTag的实体的基础:

  • 一个位于com.thalasoft.learnintouch.data下.jpa .domain包.
  • 另一个在com.thalasoft.learnintouch.data下. .domain.

从今年秋天到一个不寻常的情况,你会有Hibernate抱怨这个案子.主要是因为在运行某些HQL查询(基本上是面向实体的查询)时可能会遇到最终问题,并且可能会产生不一致的结果.

作为解决方案,您可能需要:

  • Entity使用不同的名称重命名您的bean以避免混淆,我认为这不是您的情况下的合适解决方案,因为它可能需要大量重新分解并且可能损害您的项目兼容性.

  • 配置要使用不同名称解析的EJB实体.当您使用基于xml的处理配置一个实体而另一个通过注释配置时,架构与定义实体名称不同:

    • 对于com.thalasoft.learnintouch.data.jpa.domain.TemplatePageTag实体,您需要将该name属性添加到@Entity注释中,如下所示:

      @Entity(name = "TemplatePageTag_1")
      public class TemplatePageTag extends AbstractEntity {
        //...
      }
      
      Run Code Online (Sandbox Code Playgroud)
    • 对于com.thalasoft.learnintouch.data.dao.domain.TemplatePageTag,因为它是使用hbm xml声明映射的,您需要将entity-name属性添加到class元素中,如下所示:

      <hibernate-mapping>
        <class name="com.thalasoft.learnintouch.data.dao.domain.TemplatePageTag"
          table="template_page_tag"
          entity-name="TemplatePageTag_2"
          dynamic-insert="true"
          dynamic-update="true">
      
          <!-- other attributes declaration -->
      
        </class>
      </hibernate-mapping>
      
      Run Code Online (Sandbox Code Playgroud)

当我深入研究您的项目结构时,您可能还需要修复其他bean的实体名称,因为您已经遵循了许多其他类的相同模式,例如com.thalasoft.learnintouch.data.jpa.domain.AdminModulecom.thalasoft.learnintouch.data.dao.domain.AdminModule.