Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring 4.0 with spring-data-jpa

I am using Spring 4.0 in my JavaEE application, and I tried to use the Spring-data-jpa.

However when I add the Spring-data-jpa dependency, I found that the Spring-data-jpa will depend on Spring-3.x.

Then I wonder this will cause any problem? Since my application will have Spring-4.x with Spring-3.x.

Anyone have the same experience?


Update:

I am using Spring-data-jpg-1.4.3:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.4.3.RELEASE</version>
    </dependency>

But when I run mvn dependency:tree I got this:

+- org.springframework:spring-context:jar:4.0.0.RELEASE:compile
|  +- org.springframework:spring-beans:jar:4.0.0.RELEASE:compile
|  +- org.springframework:spring-core:jar:4.0.0.RELEASE:compile
|  |  \- commons-logging:commons-logging:jar:1.1.1:compile
|  \- org.springframework:spring-expression:jar:4.0.0.RELEASE:compile
+- org.springframework:spring-aop:jar:4.0.0.RELEASE:compile
|  \- aopalliance:aopalliance:jar:1.0:compile
+- org.springframework.data:spring-data-jpa:jar:1.4.3.RELEASE:compile
|  +- org.springframework.data:spring-data-commons:jar:1.6.3.RELEASE:compile
|  +- org.springframework:spring-orm:jar:3.1.4.RELEASE:compile
|  |  \- org.springframework:spring-jdbc:jar:3.1.4.RELEASE:compile
|  +- org.springframework:spring-tx:jar:3.1.4.RELEASE:compile
|  +- org.aspectj:aspectjrt:jar:1.7.2:compile
|  +- org.slf4j:slf4j-api:jar:1.7.1:compile
|  \- org.slf4j:jcl-over-slf4j:jar:1.7.1:runtime
+- org.aspectj:aspectjweaver:jar:1.7.4:compile
+- org.springframework:spring-test:jar:4.0.0.RELEASE:test

It seems that the spring 4.0.. is mixed with spring 3.1.4..

like image 639
hguser Avatar asked Feb 12 '14 08:02

hguser


People also ask

Does spring data use JPA?

Spring Data JPA focuses on using JPA to store data in a relational database. Its most compelling feature is the ability to create repository implementations automatically, at runtime, from a repository interface. CustomerRepository extends the CrudRepository interface.

Can we use Spring Data JPA without hibernate?

Spring Data JPA is really a set of dependencies that makes it easier to work with a JPA provider. Hibernate is one of several JPA providers. This means you can use Spring Data JPA without using Hibernate (if you really wanted to).


1 Answers

This is an error in the spring-data-jpa 1.4.3.RELEASE pom denepdency.
What actually happens is that it loads spring dependencies that exist in this maven pom instead of the ones you want to import.
The short answer is add this as a parent to your maven project:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.0.0.RC5</version>
</parent>

so that it can inherit the correct dependencies.

Another way to solve this is to use the <exclusions> tag to exclude them and then import the correct dependecies but this takes more time and is not as clean. If you don't want to add spring-boot-start-parent though, then this is how to solve this error.

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.4.1.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
         ...
        </exclusion> 
    </exclusions>
</dependency>

For more info see here

like image 191
zpontikas Avatar answered Sep 26 '22 15:09

zpontikas