Skip to content

Chadiii/REST-Api-with-Spring-Boot

Repository files navigation

REST-Api-with-Spring-Boot

This small project show 4 different approach : Using Hibernate, Standard JPA, Spring Data JPA, Spring Data Rest

The Goal of this is to choose what ever Implementation and DAO techniques you want to use based on your project demands

1st Project is made using EntityManager but leverage native Hibernate API
2nd Project use EntityManager and Standard JPA Api
3rd Spring Data JPA
4st Spring Data Rest

JPA provide standard API, so you are not lock to any vendor and you can switch between them, also makes you mainting portable, flexible code and support query language : JPQL (java persistence query language)

The use of JPA Api methods is very similar to native hibernate Api :

Action Hibernate Methods JPA Methods
Create/Save new Entity session.save(...) entityManager.persist(...)
Retrieve Entity by Id session.get(...)/load(...) entityManager.find(...)
Retrieve list of Entities session.createQuery(...) entityManager.createQuery(...)
Save or Update Entity session.saveOrUpdate(...) entityManager.merge(...)
Delete Entity session.delete(...) entityManager.remove(...)

Each one of them have benefits and downside: for example if i want to create a DAO for another entity, we have to repeat all the same code again, following the same pattern.
This practice is not recommended for CRUD Rest Api, so Spring Data JPA came with a solution, by providing CRUD methods and plug it for each entity, using entity type and primary key.
Spring Data JPA provides the interface CRUDRepository, which exposes methods(some by inheritance from parents) and support some advanced features:

  • Extending and adding custom queries with JPQL
  • Query Domain specific language(Query DSL)
  • Defining custom methods

  • Method and description provided :

    • count() : Returns the number of entities available.
    • delete(T entity) : Deletes a given entity.
    • deleteAll() : Deletes all entities managed by the repository.
    • deleteAll(Iterable entities) : Deletes the given entities.
    • deleteById(ID id) : Deletes the entity with the given id.
    • existsById(ID id) : Returns whether an entity with the given id exists.
    • findAll() : Returns all instances of the type.
    • findAllById(Iterable ids) : Returns all instances of the type T with the given IDs.
    • findById(ID id) : Retrieves an entity by its id.
    • save(S entity) : Saves a given entity.
    • saveAll(Iterable entities) : Saves all given entities.

    Spring Data Rest in the other hand, provide all Spring Data Jpa features and implement the endpoints automaticly, minimizing the boiler-plate REST code and no additional coding is required !
    Previously our Architecture was : REST Controller <-> Student Service <-> Student DAO <-> Database (mySQL)
    Now with Spring Data Rest our Architecture is : Spring Data Rest <-> Student Repository <-> Database

    HTTP Method Endpoint CRUD Action
    POST /students Create a new student
    GET /students Read a list of Students
    GET /students/{studentId} Read a single student
    PUT /students/{studentId} Update an existing student
    DELETE /students/{studentId} Delete an existing student

    About

    This small project show 4 different approach : Using Hibernate, Standard JPA, Spring Data JPA, Spring Data REST

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published