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 ofIterable
'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
Why Can't Enum's Constructor Access Static Fields
Correct Way to Close Nested Streams and Writers in Java
Communication Between Two Separate Java Desktop Applications
How to Extract Cn from X509Certificate in Java
Java: How to Compile an Entire Directory Structure of Code
Reflection Type Inference on Java 8 Lambdas
Jersey Stopped Working with Injectionmanagerfactory Not Found
Java: Method to Get Position of a Match in a String
Is There a Difference Between Single and Double Quotes in Java
Javafx 2.1 Tableview Refresh Items
Difference Between Generic Type and Wildcard Type
How Is Values() Implemented for Java 6 Enums
Show Jframe in a Specific Screen in Dual Monitor Configuration
Differencebetween Identity and Equality in Oop
Looking for an Example for Inserting Content into the Response Using a Servlet Filter
What Is the Cross-Platform Way of Obtaining the Path to the Local Application Data Directory