CrudRepository Methods Example

In this post, we will see Spring Data JPA CrudRepository Methods Example using Spring Boot and Oracle. CrudRepository interface extends Repository interface. The Repository is the parent interface in the hierarchy.

CrudRepository Methods Example

CrudRepository interface has been defined as below.

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

}

The methods defined in the CrudRepository interface is as below.

  • <S extends T> save(S entity) – Used to save a single entity at a time.
  • Iterable<S> saveAll(Iterable<S> entities) – we can save multiple entities at a time.
  • Optional<T> findById(ID id) – use to get entity basis of id.
  • boolean existsById(ID id) – used to check whether an entity is already exited in DB for given Id.
  • Iterable<T> findAll() – find all entity of  paricular type.
  • Iterable<T> findAllById(Iterable<ID> ids)  – return all entity of given ids.
  • long count() – returns the number of entities
  • void deleteById(ID id) – delete the entity on basis of id
  • void delete(T entity) – delete the entity which one we are passing.
  • void delete(Iterable<? extends T> entities) – delete multiple entities which we are passing.
  • void deleteAll() – delete all entities.

<S extends T> S save(S entity).

The CrudRepository’s save() method is used to perform save as well as update operation both. If we try to save entity first time then persist() method will get invoked and if we try to update the same entity merge() will get invoked. Internal implementation of save() method.

	@Transactional
	public <S extends T> S save(S entity) {

		if (entityInformation.isNew(entity)) {
			em.persist(entity);
			return entity;
		} else {
			return em.merge(entity);
		}
	}

See more details about the CrudRepository save() method here.

Iterable<S> saveAll(Iterable<S> entities).

Used to save multiple entities. The CrudRepository saveAll() method internally uses save() method only as below.

	@Transactional
	public <S extends T> List<S> saveAll(Iterable<S> entities) {
 
		List<S> result = new ArrayList<S>();
 
		for (S entity : entities) {
		result.add(save(entity));
		}
 
		return result;
	}

See a complete example of the CrudRepository saveAll() method from scratch here.

 

Optional<T> findById(ID id).

Using findById() method we can get a single record(entity) on basis of id. Internally findById() method use EntityManger’s find() method(as below).

	public Optional<T> findById(ID id) {
 
		// some more code
 
		if (metadata == null) {
		return Optional.ofNullable(em.find(domainType, id));
		}
 
                //some more code
 
		return Optional.ofNullable(type == null ? em.find(domainType, id, hints) : em.find(domainType, id, type, hints));
	}

See a complete example of the CrudRepository findById() method from scratch here.

Iterable<T> findAll().

Used to find all entity of  particular type. The findAll() internally defined as below.

public List<T> findAll() {
return getQuery(null, Sort.unsorted()).getResultList();
}

See a complete example of CrudRepository findAll() method from scratch here.

Iterable<T> findAllById(Iterable<ID> ids).

Using findAllById() method we can get multiple records(entities) on the basis of given ids. Internally findAllById() method use findById() method only as below.

	public List<T> findAllById(Iterable<ID> ids) {
		if (entityInformation.hasCompositeId()) {
 
		List<T> results = new ArrayList<T>();
 
		for (ID id : ids) {
		findById(id).ifPresent(results::add);
		}
 
		return results;
		}
 
	}

See a complete example of the CrudRepository findAllById() method from scratch here.

long count().

The CrudRepository count() method returns the number of entities available in database. Internally count() method uses EntityManger’s createQuery() method.

	public long count() {
		return em.createQuery(getCountQueryString(), Long.class).getSingleResult();
	}

See a complete example of the CrudRepository count() method from scratch here.

void deleteById(ID id).

Using deleteById() method we can delete a single record(entity) on basis of id. If we don’t provide any id it will throw IllegalArgumentException.

	public void deleteById(ID id) {
 
		delete(findById(id));
	}

See a complete example of the CrudRepository deleteById() method from scratch here.

void delete(T entity).

Using delete() method we can delete a single record(entity). If we don’t provide any entity it will throw IllegalArgumentException.

Internally delete() method use EntityManger’s remove() method.

	public void delete(T entity) {
		em.remove(entity);
	}

See a complete example of the CrudRepository delete() method from scratch here.

void deleteAll(Iterable<? extends T> entities).

Using the above method we can delete all entities which we pass as request data. The deleteAll() internally use delete() method only.

	public void deleteAll(Iterable<? extends T> entities) {
		for (T entity : entities) {
		delete(entity);
		}
	}

See a complete example of the CrudRepository deleteAll() method from scratch here.

void deleteAll().

The above one will delete all records that belong to that repository. The deleteAll() internally uses findAll() and delete() method as below.

	public void deleteAll() {
 
		for (T element : findAll()) {
		delete(element);
		}
	}

See a complete example of the CrudRepository deleteAll() method from scratch here.

That’s all about Spring Data JPA CrudRepository Methods Example using Spring Boot and Oracle.

You may like.

Other Spring Data JPA and Hibernate tutorials.

Spring Data JPA Docs.

Summary –  We have seen what are the CrudRepository Methods defined and how these methods are internally implemented.