Spring Data Jpa EnableJpaRepositories

Spring Data JPA EnableJpaRepositories annotation is used to scan the packages for configuration and repository class for Spring Data JPA.

To understand how EnableJpaRepositories annotation works, we need to understand SpringBootApplication first. SpringBootApplication annotation is a combination of SpringBootConfiguration annotation, EnableAutoConfiguration annotation and ComponentScan annotation.

  • SpringBootConfiguration

SpringBootApplication annotation is an alternative to spring Configuration annotation. it is used to tell the application that this class is Spring Boot Configuration.

  • ComponentScan

ComponentScan annotation is used to scan directories for configuration classes. it is annotation is equivalent to Spring XML's element. it provides basePackageClasses or basePackages or it's alias value which specify which packages to scan. If packages are not defined, then the package of the configuration class annotated  ComponentScan will be considered as the default package to scan.

  • EnableAutoConfiguration

EnableAutoConfiguration annotation is used to enable spring boot automatic configuration of the application context. it will find the configured beans in the classpath. All the jars or configuration classes added in the classpath will be included in the application context.

SpringBootApplication will automatically enable the spring boot automatic configuration of the application context so adding EnableAutoConfiguration will make no difference.

It is recommended to create a configuration class if available then with SpringBootApplication or else with EnableAutoConfiguration. The package of this configuration class will be used as the default package while scanning for Entity classes.

To get maximum advantage of spring boot autoconfiguration, it is advised to put all configuration classes in the main package.

EnableAutoConfiguration requires adding all spring beans in the application context. SpringBootApplication will add all the configuration classes. to add Repository interface we required EnableJpaRepositories annotation and for Entity classes we required EntityScan annotation.

  • EnableJpaRepositories

package of the class annotated with EnableAutoConfiguration the annotation will be considered as the default package. EnableAutoConfiguration will scan the spring beans in the default package or its sub-package. if Repository the interface is defined within the default package or in a sub-package then EnableAutoConfiguration will add it into the application context, but if it's outside these packages then it needs to be included in the application context using EnableJpaRepositories annotation.

  • EntityScan

JPA Entity class also needs to be added in the application context, EnableAutoConfiguration will scan the default or sub-package but if Entity class is defined outside of the default package then it can be included in the application context using EntityScan annotation.

Apple entity class is in com.sujan.example.entity package.

package com.sujan.example.entity;
 
@Entity
public class Apple {...}

AppleRepository interface is in com.sujan.example.repository package.

package com.sujan.example.repository;

@Repository
public interface AppleRepository extends JpaRepository<Apple, Long> {
}

JpaApplication configuraiotn class is in com.sujan.example.jpa package.

package com.sujan.example.jpa;

@SpringBootApplication
public class JpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
}

Repository interface and Entity class are not defined in default package or any of its sub package hence EnableAutoConfiguration will not be able to add them in application context. There are 2 ways to make it work.

 

Move Entity class and Repository interface within the default package.

Apple entity class is in com.sujan.example.jpa.entity package.

package com.sujan.example.jpa.entity;
 
@Entity
public class Apple {...}

AppleRepository interface is in com.sujan.example.jpa.repository package.

package com.sujan.example.jpa.repository;

@Repository
public interface AppleRepository extends JpaRepository<Apple, Long> {
}

JpaApplication configuration class is in com.sujan.example.jpa; package.

package com.sujan.example.jpa;

@SpringBootApplication
public class JpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
}
body_4

manually add Entity class and Repository interface package into application context using EntityScan and EnableJpaRepositories.

package com.sujan.example.jpa;

@SpringBootApplication
@EntityScan(basePackages = {"com.sujan.example.entity"})
@EnableJpaRepositories(basePackages = "com.sujan.example.repository")
public class JpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
}

follow us on