【发布时间】:2017-12-17 06:02:48
【问题描述】:
我一直在尝试使用 Java 中的 MongoDB 和 Hibernate。我的配置文件有些问题。
我过去已经在 SQL DB 中使用过 Hibernate,但似乎 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>
但是,当我尝试创建会话时
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'.
我的方法是错误的还是我忽略了什么?
【问题讨论】:
-
AFAIR mongo 是 NoSQL 数据库。你确定 Hibernate 支持它吗?
-
hibernate.cfg.xml != persistence.xml! -
@talex 我认为应该hibernate.org/ogm
-
@Seelenvirtuose 那么设置它的正确方法是什么?我已经尝试为 Hibernate 添加一个标准的 cfg.xml 文件,但它需要指定一种方言,并且没有 NoSQL / MongoDB 选项。
标签: java mongodb hibernate jpa hibernate-ogm