Spring CrudRepository and ListCrudRepository Examples

Learn to use Spring Data’s CrudRepository and ListCrudRepository interfaces for data accessing in a Spring Boot application. We will learn to configure the Spring Data support and use these interfaces for CRUD operations.

1. Spring CrudRepository and ListCrudRepository

The CrudRepository is a generic interface for CRUD operations on a repository for a JPA entity type. It is an old interface that can be used in Spring Data 2 as well.

The ListCrudRepository is a new interface added in Spring Data 3.x. The ListCrudRepository is an extension to CrudRepository which returns a List whereas CrudRepository returns an Iterable. The goal behind its introduction is that in most cases, returning an Iterable does not offer any additional benefit over List as we ultimately have to iterate over the entities anyway.

@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {

	<S extends T> Iterable<S> saveAll(Iterable<S> entities);
	Iterable<T> findAll();
	Iterable<T> findAllById(Iterable<ID> ids);

	//other CRUD methods
}

@NoRepositoryBean
public interface ListCrudRepository<T, ID> extends CrudRepository<T, ID> {

	<S extends T> List<S> saveAll(Iterable<S> entities);
	List<T> findAll();
	List<T> findAllById(Iterable<ID> ids);
}

Since Spring Data 3.0, PagingAndSortingRepository does not extend the CrudRepository so if you are using the PagingAndSortingRepository then you need to extend from both repositories for backward compatability.

2. Setup

To setup the usage of CrudRepository and ListCrudRepository interfaces, we need to follow these steps:

2.1. Maven

All the repositories are part of Spring Data module so include it in the classpath:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
  <version>3.1.1</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
  <version>3.1.1</version>
</dependency>
<dependency>
  <groupId>org.hibernate.orm</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>6.2.5.Final</version>
</dependency>

In a Spring Boot application, we can directly add the starter dependency which transitively include all the necessary dependencies.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2.2. Configure DataSource

To configure the DataSource bean, the simplest approach is to add the properties related to the underlying database. The following properties configure a DataSource that connects to H2 database in the runtime.

spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Additionally, we need to include the dependencies for JDBC driver class according to the database used.

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
</dependency>

2.3. @Entity Class

We will also need to create JPA entities that can be persisted and retrieved from the database using the above repository interfaces.

@Entity
public class Item {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public Long id;
  public String name;

  //Setters and Getters
}

3. Using CrudRepository

To use the CrudRepository, we need to create a new interface and extend it with CrudRepository. It will automatically include all the default list and CRUD methods for our use.

import com.howtodoinjava.demo.model.Item;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ItemRepository extends CrudRepository<Item, Long> {
	
	//additonal query methods if required
	List<Item> findByName(String name);
}

Next, we can use autowire this repository in the service classes and use it.

@Autowired
ItemRepository itemRepository;

//Create Items

for (int i = 1; i <= 3; i++){
  itemRepository.save(new Item(Long.valueOf(i), "Item " + i));
}

//Retreive items

Iterable<Item> items = itemRepository.findAll();
log.info("All Items : " + items);

4. Using ListCrudRepository

Similar to CrudRepository, the ListCrudRepository also has the same usage except that it returns the List instead of Iterable.

Note that as List extends Iterable so the previous code will still work if we do not explicitly use the List type.

@Repository
public interface ItemRepository extends ListCrudRepository<Item, Long> {
	
	//...
}

//Retreive items

List<Item> items = itemRepository.findAll();

5. Conclusion

This short Spring Data tutorial discusses how to use the CrudRepository and ListCrudRepository interfaces and how to use them. It also discusses the changes made in Spring Data 3.0 to keep the compatible with previous releases.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode