3

I am trying to connect mongoDb through Spring's mongoTemplate. However, while performing CRUD operations on database, I am getting java.lang.NoSuchMethodError: org.springframework.data.mongodb.core.mapping.MongoPersistentEntity.hasVersionProperty()Z error

Here's my 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>

My mongoconfiguration file

@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;
    }
}

main class of project

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);
    }
}

If I use mongoOperation.createCollection("stores");, it works fine and a collection with name stores is getting created in database, however any mongoOperation.save() or mongoOperation.insert() method is not running.

Following 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)
1
  • Are you sure your project compiles with your updated pom.xml? I tried it and it gives compilation errors. Commented Oct 18, 2016 at 11:23

1 Answer 1

2

issuing a mvn dependency:tree -Dverbose will reveal if you have any jar conflicts. I have performed the command myself and I can see that you have mongo-java-driver to version 2.11.0 while in the org.springframework.data:spring-data-mongodb:jar:1.2.0.RELEASE references a org.mongodb:mongo-java-driver:jar:2.10.1

So make sure you have the same versions of the jars in conflict.

[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

this pom.xml works for me:

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

Make sure you clean and build your project.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks....I removed all conflicting dependencies, still error persists.
How do you run your project? Is it a jar or war? How do you build it? Do you issue a clean prior building it e.g. mvn clean package ? hasProperty() method should be available in MongoPersistentEntity superclass, if you download also the sources of your dependencies you should be able to see it.
I'll run it using a jar but to test it right now I am running it by creating main method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.