2016-10-02 75 views
1

因此,我对MongoDB和Hibernate都很陌生,我试图运行一个测试来连接和添加文档到MongoDB数据库。代码在另一台机器上运行,所以它应该可以工作,但我假设我可能在MongoDB中出现错误。尝试构建EntitiyManagerFactory时,该错误开始。我得到:休眠OGM:无法启动MongoDB数据库

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第一次运行,并且它开始监听本地主机:27017。我试着将数据库名称改为testDB,但这似乎没有什么区别。我可以通过命令行进行连接。 Heres在启动时截图。

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的大部分资源,假设你可以连接。

回答

1

尝试添加

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

的属性部分在你的persistence.xml