Spring Data : CRUD

In my previous post we saw how to setup spring data project using spring boot. We will continue on that seeing more crud actions.

To retrive just 1 record based on id. We will use findOne method. This method requires an input of the primary key, which in our case is int. Lets use it in Application.java:


ApplicationContext context =SpringApplication.run(Application.class, args);
EmployeeRepository repository = context.getBean(EmployeeRepository.class);
Employee aEmployee=repository.findOne(2);
System.out.println(aEmployee);

Outout:

spring-data-jpa-basic2

We have already seen the findAll() method that gives us all the items in the table. We ca also pass selected ids to it, si it will return only these data if present.


ApplicationContext context =SpringApplication.run(Application.class, args);
EmployeeRepository repository = context.getBean(EmployeeRepository.class);
Iterable<Employee> empList = repository.findAll(new ArrayList<Integer>(){{
add(1);
add(3);
}});
empList.forEach((emp)->System.out.println(emp));

Now we will get only 2 result of ids 1 and 3. Hibernate used the ‘in’ clause here.

Lets save a new entity:


ApplicationContext context = SpringApplication.run(Application.class, args);
EmployeeRepository repository = context.getBean(EmployeeRepository.class);

Employee emp1 = new Employee();
emp1.setFname("Max");
emp1.setLname("Payne");
emp1.setDob(new GregorianCalendar(1987, Calendar.APRIL, 3));
emp1.setSalary(150000.00);
emp1.setGender("M");

repository.save(emp1);

spring-data-jpa-basic3

We can also pass in a list of employees to the save methos and all of it will be saved (provided id is not setted).

Lets update an entity.


ApplicationContext context = SpringApplication.run(Application.class, args);
EmployeeRepository repository = context.getBean(EmployeeRepository.class);

Employee aEmployee=repository.findOne(4);
System.out.println(aEmployee.getSalary());
aEmployee.setSalary(140000.00);
repository.save(aEmployee);
System.out.println(aEmployee.getSalary());

spring-data-jpa-basic4

When we pass an entity to the save method, spring data will inspect the id of the entity to determine whether to perform an update or an insert. Since we got the data from the DB which already has the id, when we ssaved it again, spring realises that it need to do an update now.

Lets delete an entity:


ApplicationContext context = SpringApplication.run(Application.class, args);
EmployeeRepository repository = context.getBean(EmployeeRepository.class);

repository.delete(4);
System.out.println(repository.findOne(4));

spring-data-jpa-basic5

There are many other methods to delete the entites like passing a list, delete all etc. You can check the docs to see the all avaialbe method.

So these were the basic CRUD operations that we could perform. Notice that we are exposing all the methods availabe in the CrudRepository. Sometimes its not preferable to expose all like say we do not want to expose the delete methods or we want to make this a ready only repository and only expose find methods.

For this we weill first create an interface ReadOnlyRepository.java that extends Repository.


package com.mynotes.repository;

import java.io.Serializable;

import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;

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

T findOne(ID id);
Iterable<T> findAll();

}

Notice that we needed to provide type parameters. the first will just be the type and the second will be the ID and that ID needs to be serializable. We annotated this with @NoRepositoryBean beacuse since it extends the Repository interface, spring data will attempt to create an implementation of it which we do not want. We then exposed findOne and findAll methods. Make sure that these method signatures matches as that of the one in Repository.

Lets EmployeeReadOnlyRepository from it


import org.springframework.stereotype.Repository;

import com.mynotes.entity.Employee;

@Repository
public interface EmployeeReadOnlyRepository extends ReadOnlyRepository<Employee,Integer>{

}

Lets use this in Application.java: and findAll


ApplicationContext context = SpringApplication.run(Application.class, args);
EmployeeReadOnlyRepository repository = context.getBean(EmployeeReadOnlyRepository.class);
Iterable<Employee> empList = repository.findAll();
empList.forEach((emp)->System.out.println(emp));

This will print all the records in the database.

If we try to use the delete method, it will be compile time exception

spring-data-jpa-basic6

Thats it for this post. In the next we will look nto derived queries

Advertisements
%d bloggers like this: