java - 使用 MongoDB 进行 Hibernate 配置

标签 java mongodb hibernate jpa hibernate-ogm

我一直在尝试在 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/

相关文章:

java - 如何在 spring 中为扩展名为 .jsp 的 URL 提供支持

java - Hibernate 一对一单向主键 XML 映射

javascript - 如何从数据库异步获取数据?

c# - MongoDb - 使用 .NET 驱动程序运行 adminCommand (2.4.4)

java - 如何使用 Spring Data Mongo Mapper 和组合键指定键参数顺序

hibernate - Spring/Hibernate下的多个 session 工厂

java - Hibernate:在 HQL 中使用共享主键

java - 使用 JPA 的 Spring Boot 2 升级无法实例化 [com.zaxxer.hikari.HikariDataSource]

java - Android 从字节数组中保存 gif

java - Spring hatoas jaxrslinkbuilder 添加空字段