Package com.proyecto.jpa.repository
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 TypeMethodDescriptionfindByCategoryId(Long categoryId) Busca productos por categoría (usando nombre de método).Busca productos cuyo nombre contenga el texto dado (case-insensitive).findByPriceBetween(BigDecimal minPrice, BigDecimal maxPrice) Busca productos en un rango de precios.findByStockGreaterThan(Integer stock) Busca productos con stock disponible.findProductsByCategoryOrderByPrice(Long categoryId) Consulta personalizada usando JPQL (Java Persistence Query Language).findTopSellingProducts(int limit) Consulta nativa SQL.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
-
findByCategoryId
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
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
Busca productos en un rango de precios.- Parameters:
minPrice- precio mínimomaxPrice- precio máximo- Returns:
- lista de productos en ese rango
-
findByStockGreaterThan
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
-