【问题标题】:Hibernate configuration with MongoDB使用 MongoDB 进行休眠配置
【发布时间】: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


【解决方案1】:

您可以为您的配置尝试如下基本测试设置。

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "eshop" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// perform operations here
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();

【讨论】:

  • 当我尝试为我创建EntityManagerFactory 时,它显示javax.persistence.PersistenceException: No Persistence provider for EntityManager named eshop,您知道是什么原因造成的吗?
  • 我不知道,但我的persistence.xml 位于src/main/resources/META_INF 下。快速搜索将我带到这篇文章stackoverflow.com/questions/19322827/…
  • 是的,我已经看到了这些问题,我的persistence.xml 的位置正确。
【解决方案2】:

来自configure()的javadoc:

使用应用程序资源中指定的映射和属性 命名为 hibernate.cfg.xml

您正在设置persistence.xml。使用 javax.persistence.Persistence 应该可以工作:

EntityManagerFactory emf = Persistence.createEntityManagerFactory( "eshop" );

如果由于某种原因您需要会话工厂,但您正在使用 JPA,您可以使用 unwrap() 方法获取它

SessionFactory sf = emf.unwrap( SessionFactory.class );    

更新: 您也可以通过编程方式创建工厂,有一个类 OgmConfiguration(扩展了 Configuraiton):

    OgmConfiguration configuration = new OgmConfiguration();

    // This is optional, if you want to set some options using
    // a fluent API
    configuration.configureOptionsFor( MongoDB.class )
        .writeConcern( WriteConcernType.UNACKNOWLEDGED );

    SessionFactory sf = configuration
        .addAnnotatedClass( org.hsnr.rest.domain.entities.Address.class )
        // ... Other annotated classes
        .setProperty( OgmProperties.DATABASE, "mongodb_database" )
        .setProperty( OgmProperties.DATASTORE_PROVIDER, DatastoreProviderType.MONGODB.name() )

        // All this properties are optional, appropriate default will be used if missing
        .setProperty( OgmProperties.CREATE_DATABASE, "false" )
        .setProperty( OgmProperties.USERNAME, "username" )
        .setProperty( OgmProperties.PASSWORD, "password" )
        .setProperty( OgmProperties.HOST, "localhost:12897" )

        // Check MongoDBProperties for MongoDB specific options
        .setProperty( MongoDBProperties.AUTHENTICATION_MECHANISM, AuthenticationMechanismType.BEST.name() )

        .buildSessionFactory();

在此示例中,我添加了几个选项以提供概述,但如果您使用默认值并且不需要身份验证,则只需要数据库名称和数据存储提供程序

【讨论】:

  • 是否只有xml配置,我的意思是我没有找到MongoDB的java配置。
  • 如果您想知道如何以编程方式配置它,我已经更新了我的答案
  • 不错,这次更新让你的答案更丰富。
【解决方案3】:

使用下面的代码:

<hibernate-configuration
    xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

【讨论】:

  • 我尝试使用通常的hibernate.cfg.xml 文件,但它确实需要一个方言集,因此我得到了一个例外。我链接的文档还说,方言没有使用 NoSQL DB 设置(“更有趣的是不适用于 Hibernate OGM 且不应设置的选项列表:hibernate.dialect”)。有没有办法将常用的配置文件与我错过的persistence.xml 文件一起使用?
  • 我已经用另一个例子更新了我的答案,如果这没有帮助,你能在某个地方发布一个小测试项目来展示你想要做什么吗?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 2011-09-03
相关资源
最近更新 更多