🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
If you want to retrieve the maximum value of a specific column/field using Spring Data JPA, you can make use of the JPA's built-in max function. Let's see how to create a derived query method to find the maximum value.
Create JPA Entity
To demonstrate this example, assume we have the following entity Product with an attributes id, name, and price:
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Double price;
// getters, setters, etc.
}
Create a Repository
To find the maximum price for all products, here is the custom query method:
public interface ProductRepository extends JpaRepository<Product, Long> {
Optional<Product> findTopByOrderByPriceDesc();
}
Here:
findTopBy will limit the result to just one record.
OrderByPriceDesc will order the products by their price in descending order, so the first product will have the highest price.
Using the Repository in Service Class
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public Double getMaxProductPrice() {
return productRepository.findTopByOrderByPriceDesc()
.map(Product::getPrice) // Extract the price from the Product
.orElse(null);
}
}
Note that the method findTopByOrderByPriceDesc returns an Optional<Product> to handle the scenario when there are no products in the database. In such a case, the method would return an empty Optional, and the orElse(null) would convert it to a null value. Adjust as necessary based on your application's requirements.
Alternatively, if you're using a more complex query or have specific conditions, you can use the @Query annotation to define custom JPQL or native SQL queries within your repository.
For instance, here is a JPQL query to achieve the same result:
@Query("SELECT p FROM Product p WHERE p.price = (SELECT MAX(p.price) FROM Product p)")
Optional<Product> findProductWithMaxPrice();
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment