3

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.

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

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

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.

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

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.

Stephane
  • 11,836
  • 25
  • 112
  • 175
  • 1
    Could you provide projects pom files please? – tmarwen Aug 09 '14 at 18:03
  • I added the two Maven projects. I just spent a few hours, running Maven in -X debug mode, still not on any track. – Stephane Aug 09 '14 at 22:45
  • What's append when you run `mvn -Dtest=GreetingControllerTest test` from the learningtouch-rest project ? is it successful or not ? – ben75 Aug 24 '14 at 09:04
  • No it is not successful. It cannot even load the context. You can try to build it. All the source code is downloadable. – Stephane Aug 24 '14 at 09:46

2 Answers2

11

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

  • One lies under the com.thalasoft.learnintouch.data.jpa.domain package.
  • The other under the com.thalasoft.learnintouch.data.dao.domain.

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:

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

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

    • 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 {
        //...
      }
      
    • 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>
      

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.

tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • Thanks for this very detailed explanation. I already knew about the nature of the issue, as it was well described in the SO link within my question. I'm not inclined to rename all my 116 entities. Also, my question was not what the issue was, but why the issue shows up in a dependent project, the REST project, and not in the DATA project. There is something in the REST project that triggers the issue, most likely in the way the tests are configured. The tests of the DATA project pass all right. There is no reason the tests in the REST project cannot pass. – Stephane Aug 10 '14 at 18:07
  • One can see in the source code of the Maven projects that the DATA project has integration tests both against the Hibernate DAO layer and against the JPA repository layer. The tests contexts do not load both the Hibernate DAO layer and the JPA repository layer. So it's possible to have both in one project, so long as the test context does not load them both. The JPA repository layer is coming in replacement of the Hibernate DAO layer. The REST project is only trying to use the JPA repository layer. It should not try to load the context of the Hibernate DAO layer. I suspect it does though. – Stephane Aug 11 '14 at 04:29
  • Wonderful solution tmarven! – Quick_Silver Nov 27 '14 at 09:02
  • I think the trouble comes from a path issue. The data project is boasting both DAO and JPA tests and the issue does not show up. The rest project (dependent on the data project) is only boasting JPA tests but must somehow be loading the DAO entities when in fact it only needs the JPA ones. – Stephane Feb 23 '15 at 14:32
  • I tried with a `@Entity(name = "com.thalasoft.learnintouch.data.jpa.domain.ElearningCourseInfo")` on the JPA domain class, and a `entity-name="com.thalasoft.learnintouch.data.dao.domain.ElearningCourseInfo"`attribute in the DAP mapping. But it then complains that two logical entities use the same physical entity. – Stephane Dec 27 '15 at 21:51
  • I accepted the given solution for it seemed to be praised by others. As of solving my issue, I split my project in two different ones. – Stephane Dec 27 '15 at 22:40
0

This issue could be fixed by using a combination of @Entity and @Table annotations. Below link provides a good explanation and difference between both.

difference between name-attribute-in-entity-and-table

Abhishek K
  • 645
  • 1
  • 11
  • 23