Creating CRUD repositories

The Spring Boot Data JPA provides a CrudRepository interface for CRUD operations. It provides CRUD functionalities to our entity class.

We will now create our repository in the domain package as follows:

  1. Create a new class called CarRepository in the domain package and modify the file according to the following code snippet:
      package com.packt.cardatabase.domain;

import org.springframework.data.repository.CrudRepository;

      public interface CarRepository extends CrudRepository <Car, Long> {

}

Our CarRepository now extends the Spring Boot JPA CrudRepository interface. The <Car, Long> type arguments define that this is the repository for the Car entity class and the type of the ID field is Long.

CrudRepository provides multiple CRUD methods that we can now start to use. The following table lists the most commonly used methods:

 

If the method returns only one item, the Optional<T> is returned instead of T. The Optional class gets introduced in Java 8 SE. Optional is a type of single value container that either has value or doesn't. By using Optional, we can prevent null pointer exceptions.

  1. Now, we are ready to add some demonstration data to our H2 database. For that, we will use the Spring Boot CommandLineRunner interface. The CommandLineRunner interface allows us to execute additional code before the application has fully started. Therefore, it is a good point to add demo data to your database. CommandLineRunner is located inside the main class:
      import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class CardatabaseApplication {

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

@Bean

CommandLineRunner runner(){
return args -> {
// Place your code here
};
}
}
  1. Next, we have to inject our car repository into the main class to be able to save new car objects to the database. An @Autowired annotation is used to enable dependency injection. The dependency injection allows us to pass dependencies into an object. After we have injected the repository class, we can use the CRUD methods it provides. The following sample code shows how to insert a few cars to the database:
      import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.packt.cardatabase.domain.Car;
import com.packt.cardatabase.domain.CarRepository;

@SpringBootApplication
public class CardatabaseApplication {
@Autowired
private CarRepository repository;

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

@Bean

CommandLineRunner runner(){
return args -> {
// Save demo data to database
repository.save(new Car("Ford", "Mustang", "Red",
"ADF-1121", 2017, 59000));
repository.save(new Car("Nissan", "Leaf", "White",
"SSJ-3002", 2014, 29000));
repository.save(new Car("Toyota", "Prius", "Silver",
"KKO-0212", 2018, 39000));
};
}
}

The Insert statements can be seen in the Eclipse console after the application has been executed:

You can also use the H2 console to fetch cars from the database, as seen in the following screenshot:

You can define your own queries in the Spring Data repositories. The query must start with a prefix; for example, findBy. After the prefix, you define the entity class fields that are used in the query. The following is a sample code of three simple queries:

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CarRepository extends CrudRepository <Car, Long> {
// Fetch cars by brand
List<Car> findByBrand(String brand);

// Fetch cars by color
List<Car> findByColor(String color);

// Fetch cars by year
List<Car> findByYear(int year);

}

There can be multiple fields after the By keyword, concatenated with the And or Or keywords:

package com.packt.cardatabase.domain;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CarRepository extends CrudRepository <Car, Long> {
// Fetch cars by brand and model
List<Car> findByBrandAndModel(String brand, String model);

// Fetch cars by brand or color
List<Car> findByBrandOrColor(String brand, String color);
}

Queries can be sorted by using the OrderBy keyword in the query method:

package com.packt.cardatabase.domain;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CarRepository extends CrudRepository <Car, Long> {
// Fetch cars by brand and sort by year
List<Car> findByBrandOrderByYearAsc(String brand);
}

You can also create queries by using SQL statements via the @Query annotation. The following example shows the usage of a SQL query in CrudRepository:

package com.packt.cardatabase.domain;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CarRepository extends CrudRepository <Car, Long> {
// Fetch cars by brand using SQL
@Query("select c from Car c where c.brand = ?1")
List<Car> findByBrand(String brand);
}

You can also use more advanced expressions with the @Query annotation; for example, like. The following example shows the usage of the like query in CrudRepository:

package com.packt.cardatabase.domain;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CarRepository extends CrudRepository <Car, Long> {
// Fetch cars by brand using SQL
@Query("select c from Car c where c.brand like %?1")
List<Car> findByBrandEndsWith(String brand);
}

Spring Data JPA also provides PagingAndSortingRepository, which extends CrudRepository. This offers methods to fetch entities using pagination and sorting. This is a good option if you are dealing with larger amounts of data. PagingAndSortingRepository can be created in a similar way to how we created CrudRepository:

package com.packt.cardatabase.domain;

import org.springframework.data.repository.PagingAndSortingRepository;

public interface CarRepository extends PagingAndSortingRepository<Car, Long> {

}

In this case, you now have the two new additional methods that the repository provides:

Now, we have completed our first database table and we are ready to add relationships between the database tables.