Hibernate和JPA错误:在依赖的Maven项目上重复导入 [英] Hibernate and JPA error: duplicate import on dependent Maven project

查看:280
本文介绍了Hibernate和JPA错误:在依赖的Maven项目上重复导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Maven项目,一个叫做 project-data ,另一个叫 project-rest 依赖于 project-data 项目.

I have two Maven projects, one called project-data and the other one call project-rest which has a dependency on the project-data project.

Maven构建在 project-data 项目中成功,但在 project-rest 项目中失败,但以下情况除外:

The Maven build is successful in the project-data project but it fails in the project-rest project, with the exception:

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")

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

I could see some explanation here: http://isolasoftware.it/2011/10/14/hibernate-and-jpa-error-duplicate-import-try-using-auto-importfalse/

我不明白的原因是为什么在构建 project-data 项目时不会出现此消息,而在构建 project-rest 项目时会出现此消息.

What I don't understand, is why this message does not occur when building the project-data project and occurs when building the project-rest project.

我试图在 pom.xml 文件中进行查找,以查看其中是否存在可以解释该问题的内容.

I tried to look up in the pom.xml files to see if there was something in there that could explain the issue.

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

I also looked up the way the tests are configured and run on the project-rest project.

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

But I haven't yet seen any thing.

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

Adding the Maven projects: www.learnintouch.com/learnintouch-data.tar.gz www.learnintouch.com/learnintouch-rest.tar.gz

推荐答案

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

The error is basically due to the fact that the sessionFactory bean underlies two entities with the same logical name TemplatePageTag :

  • 其中一个位于 com.thalasoft.learnintouch.data. jpa .domain 软件包下.
  • com.thalasoft.learnintouch.data. dao .domain 下的另一个.
  • One lies under the com.thalasoft.learnintouch.data.jpa.domain package.
  • The other under the com.thalasoft.learnintouch.data.dao.domain.

由于今年秋天发生了一起不寻常的案件,您将有 Hibernate 对该案件进行投诉.主要是因为在运行某些 HQL 查询(基本上是面向实体的查询)时,您最终可能会遇到问题,并且结果可能不一致.

Since this fall to an unusual case, you will have Hibernate complaining about the case. Mostly because you may run in eventual issues when running some HQL queries (which are basically entity oriented queries) and may have inconsistent results.

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

As a solution, you may need either to:

  • 用不同的名称重命名Entity bean以避免混淆,我认为这对您来说不是一个合适的解决方案,因为它可能需要大量的重构,并且会损害您的项目兼容性.

  • Rename your Entity beans with different name to avoid confusion which I assume is not a suitable solution in your case since it may need much re-factoring and can hurt your project compatibility.

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

Configure your EJB entities to be resolved with different names. As you are configuring one entity using xml based processing and the other through annotation, the schema is not the same to define the entities names:

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

  • For the com.thalasoft.learnintouch.data.jpa.domain.TemplatePageTag entity, you will need to add the name attribute to the @Entity annotation as below:

@Entity(name = "TemplatePageTag_1")
public class TemplatePageTag extends AbstractEntity {
  //...
}

  • 对于 com.thalasoft.learnintouch.data.dao.domain.TemplatePageTag ,由于它是使用hbm xml声明映射的,因此您需要将entity-name属性添加到您的class元素如下:

  • For the com.thalasoft.learnintouch.data.dao.domain.TemplatePageTag, as it is mapped using an hbm xml declaration, you will need to add the entity-name attribute to your class element as follows:

    <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>
    

  • 随着我对您的项目结构的更深入研究,您可能还需要修复其他bean的实体名称,因为您对许多其他类(例如 com.thalasoft.learnintouch.data)遵循相同的架构.jpa.domain.AdminModule com.thalasoft.learnintouch.data.dao.domain.AdminModule .

    As I took a look deeper into your project strucure, you may need also to fix entity names for other beans as you have been following the same schema for many other classes, such as com.thalasoft.learnintouch.data.jpa.domain.AdminModule and com.thalasoft.learnintouch.data.dao.domain.AdminModule.

    这篇关于Hibernate和JPA错误:在依赖的Maven项目上重复导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆