MongoDB + Hibernate OGM + Spring 开机自动创建数据库

MongoDB + HibernateOGM + Spring Boot auto create database

今天,我想使用 Spring 引导用法为 MongoDB 自动创建我的数据库结构。在过去的关系数据库中,我在 application.properties 文件中使用了两个属性,例如:

spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=true

现在我想对 MongoDB 做同样的事情,但是 属性:

hibernate.ogm.datastore.create_database=true

IntelliJ 用提示标记我的 属性:

cannot resolve configuration property hibernate.ogm.datastore.create_database

我的 pom.xml 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.interview</groupId>
  <artifactId>configuration</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>configuration</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>11</java.version>
    <hibernate.version>5.4.0.Final</hibernate.version>
    <mapstruct.processor.version>1.3.0.Final</mapstruct.processor.version>
    <mapstruct.version>1.3.0.Final</mapstruct.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hibernate.ogm</groupId>
      <artifactId>hibernate-ogm-mongodb</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-processor</artifactId>
      <version>${mapstruct.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct</artifactId>
      <version>${mapstruct.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>

        <configuration>
          <source>11</source>
          <target>11</target>
          <annotationProcessorPaths>
            <path>
              <groupId>org.mapstruct</groupId>
              <artifactId>mapstruct-processor</artifactId>
              <version>${mapstruct.processor.version}</version>
            </path>
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>1.18.6</version>
            </path>
          </annotationProcessorPaths>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

我的实体标有注释@Document,但在重新运行我的应用程序后我仍然有一个空数据库。

如能提供有关如何使用 spring 引导应用程序属性用法自动创建数据库的建议,我将不胜感激。

然后 spring-boot 应用程序中等效的 Hibernate 属性应该以 spring.jpa.properties 为前缀。所以它应该是:

spring.jpa.properties.hibernate.ogm.datastore.create_database=true