Interface ProductRepository

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

@Repository public interface ProductRepository extends org.springframework.data.jpa.repository.JpaRepository<Product,Long>
Repositorio JPA para la entidad Product. Demuestra diferentes formas de crear consultas personalizadas.
  • Method Summary

    Modifier and Type
    Method
    Description
    findByCategoryId(Long categoryId)
    Busca productos por categoría (usando nombre de método).
    Busca productos cuyo nombre contenga el texto dado (case-insensitive).
    Busca productos en un rango de precios.
    Busca productos con stock disponible.
    Consulta personalizada usando JPQL (Java Persistence Query Language).
    Consulta nativa SQL.

    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

    • findByCategoryId

      List<Product> findByCategoryId(Long categoryId)
      Busca productos por categoría (usando nombre de método).
      Parameters:
      categoryId - ID de la categoría
      Returns:
      lista de productos de esa categoría
    • findByNameContainingIgnoreCase

      List<Product> findByNameContainingIgnoreCase(String name)
      Busca productos cuyo nombre contenga el texto dado (case-insensitive).
      Parameters:
      name - texto a buscar en el nombre
      Returns:
      lista de productos que coinciden
    • findByPriceBetween

      List<Product> findByPriceBetween(BigDecimal minPrice, BigDecimal maxPrice)
      Busca productos en un rango de precios.
      Parameters:
      minPrice - precio mínimo
      maxPrice - precio máximo
      Returns:
      lista de productos en ese rango
    • findByStockGreaterThan

      List<Product> findByStockGreaterThan(Integer stock)
      Busca productos con stock disponible.
      Returns:
      lista de productos con stock > 0
    • findProductsByCategoryOrderByPrice

      @Query("SELECT p FROM Product p WHERE p.category.id = :categoryId ORDER BY p.price ASC") List<Product> findProductsByCategoryOrderByPrice(@Param("categoryId") Long categoryId)
      Consulta personalizada usando JPQL (Java Persistence Query Language). Busca productos por categoría ordenados por precio.
      Parameters:
      categoryId - ID de la categoría
      Returns:
      lista de productos ordenados por precio ascendente
    • findTopSellingProducts

      @Query(value="SELECT p.* FROM products p LEFT JOIN order_items oi ON p.product_id = oi.product_id GROUP BY p.product_id ORDER BY COUNT(oi.order_item_id) DESC LIMIT :limit", nativeQuery=true) List<Product> findTopSellingProducts(@Param("limit") int limit)
      Consulta nativa SQL. Busca los productos más vendidos (top N).
      Parameters:
      limit - número de productos a retornar
      Returns:
      lista de productos más vendidos