2016-10-18 5 views
1

Ich versuche, MongoDb durch Spring MongoTemplate zu verbinden. Während jedoch die CRUD-Operationen auf Datenbank durchzuführen, erhalte ich java.lang.NoSuchMethodError: org.springframework.data.mongodb.core.mapping.MongoPersistentEntity.hasVersionProperty()Z Fehlerjava.lang.NoSuchMethodError Fehler beim Verbinden von MongoDb durch Spring

Hier ist mein pom.xml

<!-- Spring framework --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>3.2.2.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>3.2.2.RELEASE</version> 
     <exclusions> 
      <exclusion> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-core</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-beans</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

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

    <!-- Spring data mongodb --> 
    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-mongodb</artifactId> 
     <version>1.2.0.RELEASE</version> 
     <exclusions> 
      <exclusion> 
       <groupId>org.mongodb</groupId> 
       <artifactId>mongo-java-driver</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-core</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-expression</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-context</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-aop</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>aopalliance</groupId> 
       <artifactId>aopalliance</artifactId> 
      </exclusion> 
      <!-- <exclusion> 
       <groupId>org.slf4j</groupId> 
       <artifactId>slf4j-api</artifactId> 
      </exclusion> --> 
      <!-- <exclusion> 
       <groupId>org.slf4j</groupId> 
       <artifactId>jcl-over-slf4j</artifactId> 
      </exclusion> --> 
      <exclusion> 
       <artifactId>spring-beans</artifactId> 
       <groupId>org.springframework</groupId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>2.2.2</version> 
    </dependency> 

</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.0</version> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-eclipse-plugin</artifactId> 
      <version>2.9</version> 
      <configuration> 
       <downloadSources>true</downloadSources> 
       <downloadJavadocs>true</downloadJavadocs> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

Meine mongoconfiguration Datei

@Configuration 
public class SpringMongoConfig1 { 

    @Bean 
    MongoDbFactory mongoDbFactory() throws Exception{ 
     return new SimpleMongoDbFactory(new MongoClient(), "storeApp"); 
    } 

    @Bean 
    MongoTemplate mongoTemplate() throws Exception{ 
     MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory()); 
     return mongoTemplate; 
    } 
} 

Hauptklasse des Projekts

public class StoreMainApp { 

    public static void main(String[] args) { 

     System.out.println("into main method"); 

     ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig1.class); 
     MongoOperations mongoOperation = (MongoOperations)ctx.getBean("mongoTemplate"); 


     Store store1 = new Store("Sample store 1", "Street 1", "City 1", (float) 35.4); 
     System.out.println("store1 city : "+store1.storeCity); 


     mongoOperation.insert(store1); 
    } 
} 

Wenn ich mongoOperation.createCollection („stores“) verwenden ;, es funktioniert und eine Sammlung mit dem Namen speichert wird in der Datenbank erstellt zu werden, aber jede mongoOperation.save() oder mongoOperation.insert() -Methode ist nicht Laufen.

Nach Stacktrace:

Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.data.mongodb.core.mapping.MongoPersistentEntity.hasVersionProperty()Z 
     at org.springframework.data.mongodb.core.MongoTemplate.initializeVersionProperty(MongoTemplate.java:669) 
     at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:651) 
     at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:613) 
     at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:604) 
     at com.storeApp.core.StoreMainApp.main(StoreMainApp.java:33) 
+0

Sind Sie sicher, dass Ihr Projekt mit Ihrer aktualisierten pom.xml kompiliert wird? Ich habe es versucht und es gibt Kompilierungsfehler. – pleft

Antwort

1

ein mvn dependency:tree -Dverbose Ausstellung wird zeigen, wenn Sie irgendwelche jar Konflikte haben. Ich habe den Befehl selbst ausgeführt und ich kann sehen, dass Sie ein org.mongodb:mongo-java-driver:jar:2.10.1

So mongo-java-driver auf Version 2.11.0 während in den org.springframework.data:spring-data-mongodb:jar:1.2.0.RELEASE Referenzen haben sicherzustellen, dass Sie die gleichen Versionen der Gläser in Konflikt.

[INFO] +- org.springframework:spring-core:jar:3.2.2.RELEASE:compile 
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile 
[INFO] +- org.springframework:spring-context:jar:3.2.2.RELEASE:compile 
[INFO] | +- org.springframework:spring-aop:jar:3.2.2.RELEASE:compile 
[INFO] | | +- aopalliance:aopalliance:jar:1.0:compile 
[INFO] | | +- (org.springframework:spring-beans:jar:3.2.2.RELEASE:compile - omitted for duplicate) 
[INFO] | | \- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for duplicate) 
[INFO] | +- org.springframework:spring-beans:jar:3.2.2.RELEASE:compile 
[INFO] | | \- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for duplicate) 
[INFO] | +- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for duplicate) 
[INFO] | \- org.springframework:spring-expression:jar:3.2.2.RELEASE:compile 
[INFO] |  \- (org.springframework:spring-core:jar:3.2.2.RELEASE:compile - omitted for duplicate) 
[INFO] +- org.mongodb:mongo-java-driver:jar:2.11.0:compile 
[INFO] +- org.springframework.data:spring-data-mongodb:jar:1.2.0.RELEASE:compile 
[INFO] | +- org.springframework:spring-tx:jar:3.1.4.RELEASE:compile 
[INFO] | | +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate) 
[INFO] | | +- (org.springframework:spring-aop:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | | +- (org.springframework:spring-beans:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | | +- (org.springframework:spring-context:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | | \- (org.springframework:spring-core:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | +- (org.springframework:spring-context:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | +- (org.springframework:spring-beans:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | +- (org.springframework:spring-core:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | +- (org.springframework:spring-expression:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | +- org.springframework.data:spring-data-commons:jar:1.5.0.RELEASE:compile 
[INFO] | | +- (org.springframework:spring-core:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | | +- (org.springframework:spring-beans:jar:3.1.4.RELEASE:compile - omitted for conflict with 3.2.2.RELEASE) 
[INFO] | | +- (org.slf4j:slf4j-api:jar:1.7.1:compile - omitted for conflict with 1.7.21) 
[INFO] | | \- (org.slf4j:jcl-over-slf4j:jar:1.7.1:runtime - omitted for conflict with 1.7.1) 
[INFO] | +- (org.mongodb:mongo-java-driver:jar:2.10.1:compile - omitted for conflict with 2.11.0) 
[INFO] | +- (org.slf4j:slf4j-api:jar:1.7.1:compile - omitted for conflict with 1.7.21) 
[INFO] | \- (org.slf4j:jcl-over-slf4j:jar:1.7.1:compile - scope updated from runtime; omitted for duplicate) 
[INFO] \- cglib:cglib:jar:2.2.2:compile 
[INFO] \- asm:asm:jar:3.3.1:compile 

UPDATE

diese pom.xml Werke für mich:

<dependencies> 
    <!-- Spring framework --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>3.2.2.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>3.2.2.RELEASE</version> 
    </dependency> 

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

    <!-- Spring data mongodb --> 
    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-mongodb</artifactId> 
     <version>1.2.0.RELEASE</version> 

    </dependency> 

    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>2.2.2</version> 
    </dependency> 

</dependencies> 

Stellen Sie sicher, sauber und Ihr Projekt.

+0

Danke .... Ich habe alle widersprüchlichen Abhängigkeiten entfernt, immer noch besteht der Fehler. – anukuls

+0

Wie führen Sie Ihr Projekt? Ist es ein "Glas" oder "Krieg"? Wie baut man es? Geben Sie ein sauberes vorheriges Gebäude aus, z.B. 'mvn sauberes Paket'? 'hasProperty()' Methode sollte in 'MongoPersistentEntity' Superklasse verfügbar sein, wenn Sie auch die Quellen Ihrer Abhängigkeiten herunterladen, sollten Sie in der Lage sein, sie zu sehen. – pleft

+0

Ich werde es mit einem Glas laufen lassen, aber um es jetzt zu testen, führe ich es aus, indem ich die Hauptmethode erstelle. – anukuls

Verwandte Themen