Interface ShoppingCartRepository

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

@Repository public interface ShoppingCartRepository extends org.springframework.data.jpa.repository.JpaRepository<ShoppingCart, Long>
Spring Data JPA repository for ShoppingCart entities.

Provides standard CRUD operations inherited from JpaRepository and specialized query methods for shopping cart management.

The shopping cart has a strict one-to-one relationship with Customer relationship. Each customer has exactly one active cart at any time. This repository enables fast retrieval and cleanup of carts by customer association.

Key use cases:

  • Restoring cart contents after user login
  • Displaying cart summary in header/mini-cart
  • Checkout process – retrieving cart for order creation
  • Scheduled cleanup of abandoned carts
  • GDPR/account deletion – removing associated cart

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 the shopping cart associated with a specific customer.
    Finds the active shopping cart for a given customer.

    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

    • findByCustomer

      Optional<ShoppingCart> findByCustomer(Customer customer)
      Finds the active shopping cart for a given customer.

      Since the cart is uniquely tied to the customer (one-to-one), this method returns at most one result.

      Used in virtually every authenticated user request to load or update the cart.

      Parameters:
      customer - the customer whose cart to retrieve (must not be null)
      Returns:
      an Optional containing the customer's ShoppingCart if exists, or Optional.empty() if the customer has no cart (should not happen in normal flow)
    • deleteByCustomer

      void deleteByCustomer(Customer customer)
      Deletes the shopping cart associated with a specific customer.

      Used during:

      • Account deletion (GDPR compliance)
      • Manual cart reset (e.g., "Clear cart" button)
      • Admin-initiated cleanup

      Important: This method also cascades deletion of all

      invalid reference
      CartItem
      s if properly configured in the entity (orphanRemoval = true).

      Parameters:
      customer - the customer whose cart should be deleted