问题
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.
回答1:
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 yourclass
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.
回答2:
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
来源:https://stackoverflow.com/questions/25221495/hibernate-and-jpa-error-duplicate-import-on-dependent-maven-project