java - hibernate OGM : Having trouble starting MongoDB database

标签 java mongodb hibernate jpa hibernate-ogm

所以我对 MongoDB 和 Hibernate 都很陌生,我正在尝试运行一个测试来连接 MongoDB 数据库并将文档添加到其中。该代码在另一台机器上运行,因此它应该可以工作,但我假设 MongoDB 可能有错误。尝试构建 EntityManagerFactory 时出现错误。我得到:

Unable to Build entity manger factory

Caused by: Unable to start datastore provider
Caused by: OGM001219: Database testDB does not exist. Either create it yourself or set property 'hibernate.ogm.datastore.create_database' to true.

我确实先运行了 mongo,它开始监听 localhost:27017。我尝试将数据库名称更改为 testDB 但这似乎没有什么区别。我可以通过命令行进行连接。这是启动时的屏幕截图。

enter image description here

请注意,我不确定其他 3 个连接是否正常。

我的持久性文件看起来像。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="jpa-mongodb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
  <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
  <property name="hibernate.ogm.datastore.database" value="testDB"/>
  <property name="hibernate.ogm.datastore.host" value="localhost"/>
  <property name="hibernate.ogm.datastore.username" value="" />
  <property name="hibernate.ogm.datastore.password" value="" />
</properties>   

抱歉发了这么长的帖子,但我对此还不太熟悉,而且大多数有关 Hibernate 的资源似乎都假设您可以连接。

最佳答案

尝试添加

<property name="hibernate.ogm.datastore.create_database" value="true" />

到 persistence.xml 中的属性部分

关于java - hibernate OGM : Having trouble starting MongoDB database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39821399/

相关文章:

java - Android - 从任何类(class)获取位置

java - 如何准确定义域服务

java - Hibernate - 同一 session 中的第二个事务不保存修改后的对象

java - c3p0 连接池不关闭连接

java - 如何根据 build.properties 中的值配置 hibernate.cfg.xml、struts.xml、MessageResources.properties

java - 如何从android中的json url获取新的数据更新?

java - 获取字符串 url 并对其进行编码的最佳方法是什么

mongodb - 如何让 mongo 只返回字段中的数组?

mongodb - 如何正确初始化 Mongoose 集合中的文档

angularjs - angular.js - 类型错误 : Cannot read property 'then' of undefined