我一直在尝试在 Java 中使用 MongoDB 和 Hibernate。我在配置文件时遇到了一些问题。
我过去已经将 Hibernate 与 SQL DB 一起使用,但 MongoDB 的配置文件似乎必须有很大不同。
根据this documentation ,它应该看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="eshop" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>org.hsnr.rest.domain.entities.Address</class>
<class>org.hsnr.rest.domain.entities.Order</class>
<class>org.hsnr.rest.domain.entities.Person</class>
<class>org.hsnr.rest.domain.entities.Product</class>
<class>org.hsnr.rest.domain.entities.User</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
<property name="hibernate.ogm.datastore.provider"
value="mongodb" />
</properties>
</persistence-unit>
</persistence>
但是,当我尝试创建 session 时
new Configuration().configure().buildSessionFactory();
我收到以下错误:
org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 5 and column 28 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'persistence'.
我的方法是错误的还是我忽略了什么?
最佳答案
您可以针对您的配置尝试如下基本测试设置。
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "eshop" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// perform operations here
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
关于java - 使用 MongoDB 进行 Hibernate 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45063953/