5

I am trying to run a Spring webapp using hibernate 5.2.16 with struts2-core-2.3.35 on Tomcat8.5 with JDK8.

I am getting the below exception :

Caused by: org.hibernate.DuplicateMappingException: Duplicate query mapping getDocExt
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.checkQueryName(InFlightMetadataCollectorImpl.java:524) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.applyNamedQuery(InFlightMetadataCollectorImpl.java:518) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addNamedQuery(InFlightMetadataCollectorImpl.java:514) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.model.source.internal.hbm.NamedQueryBinder.processNamedQuery(NamedQueryBinder.java:75) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.model.source.internal.hbm.NamedQueryBinder.processNamedQuery(NamedQueryBinder.java:39) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.model.source.internal.hbm.MappingDocument.processNamedQueries(MappingDocument.java:198) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.model.source.internal.hbm.HbmMetadataSourceProcessorImpl.processNamedQueries(HbmMetadataSourceProcessorImpl.java:94) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processNamedQueries(MetadataBuildingProcess.java:173) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:269) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:511) ~[spring-orm-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:495) ~[spring-orm-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1688) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    ... 24 more

When I looked up for this exception, most answers were related to

org.hibernate.DuplicateMappingException: Duplicate class/entity mapping

This is different as the exception I am getting does not have "Duplicate class/entity mapping". Instead for me its "Duplicate query mapping".

What could be the reason for getting this exception.

DockYard
  • 989
  • 2
  • 12
  • 29

3 Answers3

6

Caused by: org.hibernate.DuplicateMappingException: Duplicate query mapping getDocExt

This line in the stack trace clearly indicates that you have accidentally defined two named queries with the same name - getDocExt. These two named queries with the same name don't necessarily have to be on the same entity for this error to occur. Even if these duplicate named queries on declared on two different entities, you would still get this error. This is because the scope of a named query is the entire persistence unit. Figure out the duplicate named queries and choose different names. As a good programming practice, choose name such as <entity>.getDocExt instead of just getDocExt. If you employ this practice, you will never have this error.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • In my case, I had copy/pasted the same NamedQueries into two different entities/files. Changed the query parameters but missed the names were the same. – stackbacker Jan 26 '21 at 01:28
1

One reason this can happen due to use of both config files and annotations. Check whether you have defined your mappings in

hibernate config file
annotations - java class

eg: in hibernate config file

<mapping class="com.xxx.City" />

In Java

@Entity
@Table(name = "city_master")
public class City implements Serializable {

}
Amal
  • 9
  • 2
0

This exception occur due to duplicate name of named query like below.

<named-query name="getDocExt">
<query>
    some query
</query>
</named-query>

<named-query name="getDocExt">
<query>
    some query
</query>
</named-query>

Note: named query name should be unique

Eswar
  • 1
  • 1