Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodError Error while connecting mongoDb through Spring

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)
like image 210
anukuls Avatar asked Apr 20 '26 15:04

anukuls


1 Answers

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.

like image 76
pleft Avatar answered Apr 22 '26 06:04

pleft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!