Search code examples
javaspring-bootspring-data-jpaspring-datajpa

javax.persistence not included in spring data jpa?


I just created a maven project with this dependency

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

but I realized that all these classes are not found:

import javax.persistence.*

so I have to change the dependency to

 <dependency>
                <groupId>javax.persistence</groupId>
                <artifactId>javax.persistence-api</artifactId>
                <version>2.2</version>
            </dependency>

which I find it weird because last update is from Aug 21, 2017

there is no spring dependency that includes javax.persistence ????


Solution

  • Spring Data JPA itself isn't an implementation of the JPA specification. It is an abstraction built on top of JPA that requires a JPA implementation, typically Hibernate, to function. The JPA implementation provides the javax.persistence classes. For this reason, with a dependency on org.springframework.data:spring-data-jpa alone, the javax.persistence classes will not be on the classpath.

    If you're using Spring Boot's Data JPA starter (org.springframework.boot:spring-boot-starter-data-jpa), it has dependencies on both Spring Data JPA and Hibernate. Hibernate then has a dependency on the javax.persistence API which makes its classes available to your application's code.