Skip to content

Spring Boot - Soft Deletes functionality with Spring Data Rest, JPA Repositories, Hibernate. Custom JPA Repository implementation for usage of soft deletes using LocalDateTime.

License

dzinot/spring-boot-jpa-data-rest-soft-delete

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot JPA Soft Deletes with Spring Data Rest

Simple example of how to use custom JPA repository implementation for soft deletes functionality.

List of Things

  • Service layer
  • Project Lombok
  • Controller layer
  • Repository layer
  • Entity Validation
  • JSON Configuration
  • usage of JPA Specifications
  • generic soft delete functionality
  • custom JPA Repository implementation
  • generic schedule soft delete functionality
  • association table entities are with @IdClass
  • Spring Data Rest endpoints merged with custom controller endpoints
  • generic methods to use in repositories (see the methods from the interface below)
  • on creation of new entity check whether a soft deleted version already exists in the database
  • BackendIdConverter for every association entity so you can use Spring Data REST to manipulate entity
    • http GET localhost:8080/roleUsers/1_1
    • http DELETE localhost:8080/roleUsers/1_1 // TODO
    • http PATCH localhost:8080/roleUsers/1_1 otherField="field"
  • use @UniqueConstraint inside @Table to define unique constraints (MUST MATCH DATABASE SCHEMA)
    • @Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "single" }) })
    • @Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "single1" }), @UniqueConstraint(columnNames = { "single2" }) })
    • @Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "double1", "double2" }) })
  • CustomRepositoryRestConfigurerAdapter to register the BackendIdConverters and expose the ID fields for all entities
  • integration of the entity check functionality on create with Spring Data REST, the check is done on Spring Data REST repository endpoints POST methods

TODO

  • write tests
  • write examples and usage
  • write documentation about everything
  • make generic repository methods accessable trough Spring Data Rest endpoints

Notes

  • Because of using Lombok, mapped entities should be excluded from the toString() method
    • check the User entity for example
    @ToString(exclude = { "roles" })

Generic Repository Methods

long countActive();
T findOneActive(ID id);
boolean existsActive(ID id);
Iterable<T> findAllActive();
Iterable<T> findAllActive(Sort sort);
Page<T> findAllActive(Pageable pageable);
Iterable<T> findAllActive(Iterable<ID> ids);
void softDeleteAll();
void softDelete(ID id);
void softDelete(T entity);
void softDelete(Iterable<? extends T> entities);
void scheduleSoftDelete(ID id, LocalDateTime localDateTime);
void scheduleSoftDelete(T entity, LocalDateTime localDateTime);

Entity mapping examples

//

Example HTTP calls

//

In Short

In order to use this, we first must create our CustomJpaRepositoryFactoryBean and enable that in our main class using:

@EnableJpaRepositories(repositoryFactoryBeanClass = CustomJpaRepositoryFactoryBean.class)

This returns our custom SoftDeletesRepositoryImpl instead of the default SimpleJpaRepository.

There's a BaseEntity which all of our entities must extend, that contains the necessary field to enable us to use this functionality.

Now for every repository interface, instead of extending the JpaRepository we extend our SoftDeletesRepository (UserRepository example).

Installing

Just clone or download the repo and import it as an existing maven project.

You'll also need to set up Project Lombok or if you don't want to use this library you can remove the associated annotations from the code and write the getters, setters, constructors, etc. by yourself.

License

This project is licensed under the MIT License - see the LICENSE file for details

About

Spring Boot - Soft Deletes functionality with Spring Data Rest, JPA Repositories, Hibernate. Custom JPA Repository implementation for usage of soft deletes using LocalDateTime.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages