- In my web app I need to connect to Mongodb and Neo4j, how to configure it?
- and if I need to connect to Mongodb and Mysql , how to configure it?
Asked
Active
Viewed 219 times
0

Anju Muralidharan
- 647
- 6
- 18

Qs Lin
- 1
1 Answers
0
You need to define different persistence units. For example, if you are using the persistence.xml file it will look something like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="neo4j-pu">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.ogm.datastore.provider" value="neo4j_http"/>
...
</properties>
</persistence-unit>
<persistence-unit name="mongodb-pu">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.ogm.datastore.provider" value="MONGODB"/>
<property name="hibernate.ogm.datastore.database" value="ogm_test_database"/>
<property name="hibernate.ogm.datastore.create_database" value="true"/>
...
</properties>
</persistence-unit>
<persistence-unit name="mysql-pu">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpatestdb" />
...
</properties>
</persistence-unit>
</persistence>
At this point when you need the persistence factory or the entity manager you need to specify the name:
@PersistenceUnit(unitName = "neo4j-pu")
EntityManagerFactory neo4jEMF;
@PersistenceContext(unitName = "neo4j-pu")
EntityManager neo4jEM;
This isn't any different from what you would do with Hibernate ORM only or JPA. You can also do this via native API, but I think you got the idea.

Davide D'Alto
- 7,421
- 2
- 16
- 30