Spring Data JPA Links (Bookmark them!)

  1. API Docs:

    https://docs.spring.io/spring-data/data-jpa/docs/current/api/

  2. Ref docs:

    https://docs.spring.io/spring-data/jpa/docs/current-SNAPSHOT/reference/html/#reference

  3. Spring Java Docs:

    Link: https://docs.spring.io/spring-framework/docs/current/javadoc-api/

More Practice of Spring Data JPA

Solve

  1. List all veg Food Items

    Add a method in FoodItemDao extends JpaRepository<FoodItem, Long>:

    List<FoodItem> findByIsVegTrue();
    
    
  2. Get restaurant and food items details

    Input: Restaurant ID

    Add a method in RestaurantDao extends JpaRepository<Restaurant, Long>:

    @Query("select r from Restaurant r left join fetch r.foodItems where r.id = :restaurantId")
    Optional<Restaurant> getCompleDetails(Long restaurantId);
    
    
  3. Update price and description of a food item by its name and restaurant id

    Input: Food item name and restaurant ID (unique), new price, and new description

    3.1

    Optional<FoodItem> findByItemNameAndMyRestaurantId(String name, Long restaurantId);
    
    

    3.2 Invoke it from service layer → orElseThrow → call setters → upon service layer method returns → tx.commit → update (DML) → controller.

    OR

    @Query("update FoodItem f set f.price=:pr , f.description=:desc where f.ItemName=:nm and f.myRestaurant.id=:id")
    @Modifying
    int | long | void updateFoodItemDetails(double pr, String desc, String nm, Long id);
    
    
  4. Delete product by its name

    Input: Product name (unique)

    DAO: ProductDao

    int deleteByName(String nm);
    
    
  5. List all products having price less than specified price and from specified category (by category name)

    DAO: ProductDao

    List<Product> findByPriceLessThanAndMyCategoryName(double maxPrice, String catName);
    
    
  6. Apply specified % discount on all products from specified category id, which have not been updated recently

    Input: Discount in %, category ID, number of days (not purchased)

    In ProductServiceImpl:

4. Enter REST

0. REST Basics

1. Understand with a Diagram

1.1 Back end development of full stack application

1.2 Annotations