# Creating 2 session factories throw org.hibernate.DuplicateMappingException: Duplicate query mapping

**URL:** <https://discourse.hibernate.org/t/creating-2-session-factories-throw-org-hibernate-duplicatemappingexception-duplicate-query-mapping/5450>\
**Category:** Hibernate ORM\
**Created:** [June 19, 2021, 6:18pm UTC](https://discourse.hibernate.org/t/creating-2-session-factories-throw-org-hibernate-duplicatemappingexception-duplicate-query-mapping/5450 "2021-06-19T18:18:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![someGuy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/someguy/32/4035_2.png) [@someGuy](https://discourse.hibernate.org/u/someGuy)\
**Post date:** [June 19, 2021, 6:18pm UTC](https://discourse.hibernate.org/t/creating-2-session-factories-throw-org-hibernate-duplicatemappingexception-duplicate-query-mapping/5450/1 "2021-06-19T18:18:50Z")

</div>

Hi,

I’ve a legacy code that create 2 SessionFactory for the same database (for some unkown reason)  
and it works , thatis 2 session factories are created in version 3.x

all entites are in hbm format for now

when I use the same code in version 5.4.x I get the following excption:  
org.hibernate.DuplicateMappingException: Duplicate query mapping NAMES\_QUERY\_NAME

can some explain me why I get this error since I have only ine named query with that name or what have changed in version 5.X? Moreover when I use JPA annotation instead of hbm I don’t get this error.

And is there any reason to use 2 session factories for the same DB (one DB in system)

Thank u in advance

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 21, 2021, 7:50am UTC](https://discourse.hibernate.org/t/creating-2-session-factories-throw-org-hibernate-duplicatemappingexception-duplicate-query-mapping/5450/2 "2021-06-21T07:50:17Z")

</div>

Maybe you include the same file multiple times? One possible reason for using multiple session factories is when you want to use a different data source for the second session factory. One could use a data source with read committed isolation level and the other serializable. Or one could be connecting to the primary whereas the other connects to a read-only secondary database.

---

<div class="post-metadata">

**Author:** ![someGuy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/someguy/32/4035_2.png) [@someGuy](https://discourse.hibernate.org/u/someGuy)\
**Post date:** [June 25, 2021, 7:23am UTC](https://discourse.hibernate.org/t/creating-2-session-factories-throw-org-hibernate-duplicatemappingexception-duplicate-query-mapping/5450/3 "2021-06-25T07:23:54Z")

</div>

Thank you for answering

I figured it out. I have only one hbm file. The thing is I use the same Configuration objet for both SessionFactories and from debugging I see the same instance StandardServiceRegistry is accesed with calling buildSessionFactory() method.

To solve it I cearted 2 different instanes of Configuration class and called on each of them buildSessionFactory();
