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>
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 TypeMethodDescriptionvoiddeleteByCart(ShoppingCart cart) Deletes all items from a cart — used on "Clear Cart" or account deletion.booleanexistsByCartAndProduct(ShoppingCart cart, Product product) Checks if a product is already in the customer's cart.findByCart(ShoppingCart cart) Finds all shopping items belonging to a specific shopping cart.findByCartAndProduct(ShoppingCart cart, Product product) Finds a specific item in a specific cart by product — the most common operation.findByProduct(Product product) 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, saveMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlushMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findByProduct
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
ShoppingItemper 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
Optionalcontaining the matchingShoppingItemif found, orOptional.empty()if the product is not in any cart
-
findByCart
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
Finds a specific item in a specific cart by product — the most common operation. Prevents duplicate entries and enables quantity updates. -
deleteByCart
Deletes all items from a cart — used on "Clear Cart" or account deletion. -
existsByCartAndProduct
Checks if a product is already in the customer's cart.
-