What Is Difference Between Crudrepository and JPArepository Interfaces in Spring Data JPA

What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?

JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository.

Their main functions are:

  • CrudRepository mainly provides CRUD functions.
  • PagingAndSortingRepository provides methods to do pagination and sorting records.
  • JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.

Because of the inheritance mentioned above, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository. So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository , use CrudRepository.

What exactly is the difference between JpaRepository and CrudRepository using Spring Data JPA?

Note that JpaRepository extends CrudRepository. Compare the JavaDoc of these two interfaces:

JpaRepository vs CrudRepository

In short JpaRepository

  • has additional JPA specific methods that support for example Query
    By Example , deleting in batches, manual flushing changes to database
  • querying methods return List's instead of Iterable's

If you are using JPA you should use JpaRepository.

JpaRepository vs CRUDRepository findAll

The class CrudRepository is part of the Spring Data Commons project and is the recommended interface to extend regardless of the actual data store used.

The reason CrudRepository methods return Iterable and not List (or Set) is because some data stores allow streaming of results and using a Collection type would result in loss of functionality for such stores.

Found 0 JPA repository interfaces after I ran my spring boot application

Since your AujhelpdeskApplication is in package auj.helpdesk.starter;, Spring boot will only scan packages that start with auj.helpdesk.starter, which means it will skip auj.helpdesk.repository because they are siblings, repository does not lie inside auj.helpdesk.starter package.

package auj.helpdesk.starter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AujhelpdeskApplication {

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

}

To address this, you either have to move your main class to package auj.helpdesk so that containing package is parent to all, or you have to specify locations where you want components to look into.

package auj.helpdesk.starter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "auj.helpdesk")
public class AujhelpdeskApplication {

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

}


Related Topics



Leave a reply



Submit