Interface ShoppingItemRepository

All Superinterfaces:
org.springframework.data.repository.CrudRepository<ShoppingItem, Long>, org.springframework.data.jpa.repository.JpaRepository<ShoppingItem, Long>, org.springframework.data.repository.ListCrudRepository<ShoppingItem, Long>, org.springframework.data.repository.ListPagingAndSortingRepository<ShoppingItem, Long>, org.springframework.data.repository.PagingAndSortingRepository<ShoppingItem, Long>, org.springframework.data.repository.query.QueryByExampleExecutor<ShoppingItem>, org.springframework.data.repository.Repository<ShoppingItem, Long>

@Repository public interface ShoppingItemRepository extends org.springframework.data.jpa.repository.JpaRepository<ShoppingItem, Long>
Spring Data JPA repository for ShoppingItem entities (also known as CartItem).

Provides standard CRUD operations inherited from JpaRepository and specialized query methods for managing items within a customer's shopping cart.

A ShoppingItem represents a single product entry in a ShoppingCart with a specific quantity. The combination of cart and product must be unique — a customer cannot add the same product twice.

This repository is essential for:

  • Loading cart contents on every authenticated request
  • Adding, updating, or removing items from the cart
  • Validating stock availability during checkout
  • Calculating cart totals and applying promotions

This repository is automatically implemented by Spring at runtime — no manual implementation required.

Since:
1.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Deletes all items from a cart — used on "Clear Cart" or account deletion.
    boolean
    Checks if a product is already in the customer's cart.
    Finds all shopping items belonging to a specific shopping cart.
    Finds a specific item in a specific cart by product — the most common operation.
    Finds a shopping cart item by the associated product.

    Methods inherited from interface org.springframework.data.repository.CrudRepository

    count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, save

    Methods inherited from interface org.springframework.data.jpa.repository.JpaRepository

    deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlush

    Methods inherited from interface org.springframework.data.repository.ListCrudRepository

    findAll, findAllById, saveAll

    Methods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository

    findAll

    Methods inherited from interface org.springframework.data.repository.PagingAndSortingRepository

    findAll

    Methods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor

    count, exists, findAll, findBy, findOne
  • Method Details

    • findByProduct

      Optional<ShoppingItem> findByProduct(Product product)
      Finds a shopping cart item by the associated product.

      Used to check if a product is already in the cart before adding it again. In a properly designed system, only one ShoppingItem per product should exist in a given cart.

      Typical use case: "Add to Cart" button — increase quantity if exists, create new item if not.

      Parameters:
      product - the product to search for in any cart
      Returns:
      an Optional containing the matching ShoppingItem if found, or Optional.empty() if the product is not in any cart
    • findByCart

      List<ShoppingItem> findByCart(ShoppingCart cart)
      Finds all shopping items belonging to a specific shopping cart.

      This is the primary method for loading the full contents of a customer's cart. Usually ordered by creation date or product name in the service layer.

      Parameters:
      cart - the shopping cart to retrieve items from (must not be null)
      Returns:
      a list of all ShoppingItems in the cart (may be empty)
    • findByCartAndProduct

      Optional<ShoppingItem> findByCartAndProduct(ShoppingCart cart, Product product)
      Finds a specific item in a specific cart by product — the most common operation. Prevents duplicate entries and enables quantity updates.
    • deleteByCart

      void deleteByCart(ShoppingCart cart)
      Deletes all items from a cart — used on "Clear Cart" or account deletion.
    • existsByCartAndProduct

      boolean existsByCartAndProduct(ShoppingCart cart, Product product)
      Checks if a product is already in the customer's cart.