答案 0 :(得分:0)
您需要定义不同的持久性单元。 例如,如果您使用的是persistence.xml文件,它将类似于:
<?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>
此时需要持久性工厂或实体管理器时,需要指定名称:
@PersistenceUnit(unitName = "neo4j-pu")
EntityManagerFactory neo4jEMF;
@PersistenceContext(unitName = "neo4j-pu")
EntityManager neo4jEM;
这与使用Hibernate ORM或JPA所做的没有任何不同。您也可以通过本机API执行此操作,但我认为您有这个想法。