所以我对 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 但这似乎没有什么区别。我可以通过命令行进行连接。这是启动时的屏幕截图。
请注意,我不确定其他 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/