【问题标题】:Hibernate-OGM-MongoDB: how to create/update collections on start-up?Hibernate-OGM-MongoDB:如何在启动时创建/更新集合?
【发布时间】:2019-04-02 14:09:19
【问题描述】:

我正在尝试在 Spring-Boot 应用程序中为 MongoDB 配置 Hibernate-OGM,但它没有在本地运行的数据库中创建相应的集合。

这是我的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>eu.phishbowl</groupId>
    <artifactId>downbox</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>downbox</name>
    <description>Downbox</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--This will make sure that you are using matching versions
            of the Hibernate OGM modules and their dependencies.-->
            <dependency>
                <groupId>org.hibernate.ogm</groupId>
                <artifactId>hibernate-ogm-bom</artifactId>
                <version>5.4.1.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!--Spring-Boot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>

        <!--Mail-->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

        <!--Hibernate-OGM-->
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jboss.narayana.jta</groupId>
            <artifactId>narayana-jta</artifactId>
            <version>5.9.2.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

这是我的application.yml:

spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: downbox
      username: admin
      password: admin
      authentication-database: admin
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: create

这是主类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DownboxApplication {

    public static void main(String[] args) {
        SpringApplication.run(DownboxApplication.class, args);
    }
}

蚂蚁终于是我的实体了:

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Entity
@Getter
@Setter
public class Email {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "email")
    private Long id;
    private String from;
    private String to;
    private String object;
    private byte[] eml;
}

几天以来我一直坚持这一点,但并没有真正理解这里出了什么问题。我是否缺少某些依赖项或某些应用程序属性?

【问题讨论】:

    标签: java mongodb spring-boot jpa hibernate-ogm


    【解决方案1】:

    Hibernate OGM 和 Spring Data 是两个不同的库。我可以在 application.yml 中看到您正在设置 Spring Data 的属性,但我没有看到 Hibernate OGM 的配置(通常在 persistence.xml 中)。 我不认为 Hibernate OGM 正在启动。

    以下是 OGM 的 persistence.xml 示例:

    <persistence-unit name="hike-PU" transaction-type="JTA">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
    
        <properties>
            <property name="hibernate.ogm.datastore.provider" value="MONGODB" />
            <property name="hibernate.ogm.datastore.database" value="hike_db" />
            <property name="hibernate.ogm.datastore.create_database" value="true" />
        </properties>
    </persistence-unit>
    

    来自这个项目演示:https://github.com/DavideD/hibernate-demos/tree/prova/hibernate-ogm/nosql-with-hibernate-ogm-101/hibernate-ogm-demo-nosql-with-hibernate-ogm-101-part-3

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多