javamongodbspring-boothibernate-ogm

MongoDB + HibernateOGM + Spring Boot auto create database


Today, I want to auto-create my database structure for MongoDB with Spring Boot usage. In relational databases in the past, I was using two properties in the application.properties file like:

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

Now I want to do the same for MongoDB but with property:

hibernate.ogm.datastore.create_database=true

IntelliJ marking my property with the hint:

cannot resolve configuration property hibernate.ogm.datastore.create_database

My pom.xml looks like:

<?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>

My entity is marked with annotation @Document but still after rerunning my application I have an empty database.

I will be grateful for a suggestions on how to auto-create database with spring boot application properties usage.


Solution

  • Then equivalent Hibernate properties in spring-boot application should be prefix with spring.jpa.properties .So it should be:

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