Morphia 在保存到 DataStore 时给出 NoSuchMethodError

Morphia gives NoSuchMethodError while saving to DataStore

所以我正在尝试将 Morphia 用于 MongoDB ORM。早些时候,我们总是直接为 CRUD 使用 mongo-java-driver,但 ORM 显然更有意义。

我们的 mongo-java-driver 是 3.1.0 版,我正在使用 morphia 当前版本 - 1.3.2。

我执行了一个简单的测试来启动它并使用以下代码在测试项目中工作:

public class MorphiaTest {

  private Morphia morphia;
  private Datastore datastore;

  public MorphiaTest() {
    this.morphia = new Morphia();

    // should contain all the entity classes that we want to morphia to map
    morphia.mapPackage("com.test.entity");

    MongoClientOptions options = new MongoClientOptions.Builder().build();
    ServerAddress address = new ServerAddress("127.0.0.1", 27017);
    MongoClient client = new MongoClient(address, options);

    // configure morphia once and use different Datastore instances to connect to multiple databases
    this.datastore = morphia.createDatastore(client, "test");
  }

  public static void main(String[] args) {
    MorphiaTest morphia = new MorphiaTest();
    GroupProfile profile = new GroupProfile();
    profile.setName("First test group");
    profile.setDescription("Desc for first test group");
    profile.setTs(System.currentTimeMillis());
    profile.setType(0);
    morphia.datastore.save(profile);
  }

GroupProfile 是一个 Entity,我想将其写入 mongo 中 test 数据库中的集合,并且相当简单。

当我 运行 出现以下错误时:

INFO: Opened connection [connectionId{localValue:1, serverValue:2}] to 127.0.0.1:27017
Nov 07, 2017 2:48:50 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 15]}, minWireVersion=0, maxWireVersion=3, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=554323}
Exception in thread "main" java.lang.NoSuchMethodError: com.mongodb.InsertOptions.getBypassDocumentValidation()Ljava/lang/Boolean;
    at org.mongodb.morphia.InsertOptions.getBypassDocumentValidation(InsertOptions.java:93)
    at org.mongodb.morphia.InsertOptions.copy(InsertOptions.java:36)
    at org.mongodb.morphia.DatastoreImpl.enforceWriteConcern(DatastoreImpl.java:1245)
    at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:775)
    at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:758)

谁能告诉我哪里错了?

这是我的 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>com.bsb.test.morphia</groupId>
  <artifactId>morphiatest</artifactId>
  <version>0.1</version>
  <packaging>pom</packaging>
  <url>http://maven.apache.org</url>
  <name>${project.artifactId}-${project.version}</name>

  <dependencies>
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.mongodb.morphia</groupId>
      <artifactId>morphia</artifactId>
      <version>1.3.2</version>
      <exclusions>
        <exclusion>
          <groupId>org.mongodb</groupId>
          <artifactId>mongo-java-driver</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>versions-maven-plugin</artifactId>
        <version>2.3</version>
      </plugin>
      <plugin>
        <groupId>com.coveo</groupId>
        <artifactId>fmt-maven-plugin</artifactId>
        <version>1.8.0</version>
      </plugin>

    </plugins>
  </build>

</project> 

我可能弄乱了吗啡和 mongo-java-驱动程序之间的依赖关系,但似乎无法弄清楚。

Morphia 1.3.2 与 Mongo Java Driver 3.5.0 兼容。 Morphia 1.3.2 JAR 将传递该依赖项。您正在通过此排除来避免传递依赖:

<dependency>
  <groupId>org.mongodb.morphia</groupId>
  <artifactId>morphia</artifactId>
  <version>1.3.2</version>
  <exclusions>
    <exclusion>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
    </exclusion>
  </exclusions>
</dependency>

相反,您应该删除此依赖项:

<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongo-java-driver</artifactId>
  <version>3.1.0</version>
</dependency>

... 并接受 Morphia:1.3.2 兼容的 Mongo Java 驱动程序版本。