2016-10-02 71 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