# Configuration of two Mongo databases

**URL:** <https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442>\
**Category:** Hibernate OGM\
**Created:** [November 14, 2019, 8:10am UTC](https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442 "2019-11-14T08:10:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Faizan\_Riaz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/faizan_riaz/32/648_2.png) [@Faizan\_Riaz](https://discourse.hibernate.org/u/Faizan_Riaz)\
**Post date:** [November 14, 2019, 8:10am UTC](https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442/1 "2019-11-14T08:10:22Z")

</div>

Hi,

I want to configure two mongo databases. how can I define it in the Persistence.xml file?

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [November 14, 2019, 10:52am UTC](https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442/2 "2019-11-14T10:52:40Z")

</div>

You need to create two distinct persistence units for this case:

```auto

<persistence ...>
    <persistence-unit name="mongoPU-1" transaction-type="JTA">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <properties>
            ...
        </properties>
    </persistence-unit>
    <persistence-unit name="mongoPU-2" transaction-type="JTA">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>        
        <properties>
            ...
        </properties>
    </persistence-unit>
</persistence>

```

Then in your code you can access the one you need with the annotation:

```auto
@PersistenceUnit(uniteName="mongoPU-2")
EntityManagerFactory ef;

```

or

```auto
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "mongoPU-1 );

```

Note that they are two distinct persistence units and you cannot, for example, have a join on two entities that are not in the same factory.

I hope this help.

---

<div class="post-metadata">

**Author:** ![Faizan\_Riaz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/faizan_riaz/32/648_2.png) [@Faizan\_Riaz](https://discourse.hibernate.org/u/Faizan_Riaz)\
**Post date:** [November 14, 2019, 12:34pm UTC](https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442/3 "2019-11-14T12:34:36Z")

</div>

thank you for your quick response. this works perfectly in my project but now i have another issue in my project i have a some object variables, when i want to perform a query it gives me the error that type is wrong.

e.g  
private object environment;  
private object extension;

unders these object i also some nested object, should i have to perform this query in native language or i can also use JPQL?

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [November 14, 2019, 1:18pm UTC](https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442/4 "2019-11-14T13:18:55Z")

</div>

Hibernat OGM doesn’t know how to convert the object and that’s why you see an error.  
Supported built-in type are here: [https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html\_single/#mongodb-built-in-types](https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#mongodb-built-in-types)

You could use a native query but a better solution would be to change the mapping of your entities using the appropriate type. If the document are nested you can map them as embedded entities: [https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html\_single/#\_embedded\_objects\_and\_collections\_2](https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#_embedded_objects_and_collections_2)

One example from the documentation:

```auto

@Entity
public class News {

    @Id
    private String id;
    private String title;

    @Embedded
    private NewsPaper paper;

    // getters, setters ...
}

@Embeddable
public class NewsPaper {

    private String name;
    private String owner;

    // getters, setters ...
}

```

---

<div class="post-metadata">

**Author:** ![Faizan\_Riaz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/faizan_riaz/32/648_2.png) [@Faizan\_Riaz](https://discourse.hibernate.org/u/Faizan_Riaz)\
**Post date:** [November 15, 2019, 8:13am UTC](https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442/5 "2019-11-15T08:13:32Z")

</div>

i have a one more question, is this modle also work with SQL or it is only for NoSQL databases?

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [November 15, 2019, 10:12am UTC](https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442/6 "2019-11-15T10:12:50Z")

</div>

Yes, that’s the idea around Hibernate OGM.

If you use the JPA annotations and JPQL it doesn’t matter which datastore you are using (in theory).

Moving from a nosql datastore to a relational one, should work fine.

---

<div class="post-metadata">

**Author:** ![Faizan\_Riaz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/faizan_riaz/32/648_2.png) [@Faizan\_Riaz](https://discourse.hibernate.org/u/Faizan_Riaz)\
**Post date:** [November 15, 2019, 1:24pm UTC](https://discourse.hibernate.org/t/configuration-of-two-mongo-databases/3442/7 "2019-11-15T13:24:40Z")

</div>

Thank you very much for your help.
