To pass a List (or any Collection) into a Spring Data JPA repository query, you can use the IN clause in JPQL or native SQL, and Spring will automatically bind the list elements. ✅ Example 1: Using @Query with JPQL @Query(“SELECT p FROM Product p WHERE p.id IN :ids”) List<Product> findByIds(@Param(“ids”) List<Long> ids); You can…
Category: Spring
How to return HashMap from Spring Repository
A Spring Data Repository typically returns entities or List of Object arrays List<Object[]> – for custom queries, not collections like HashMap<K, V> directly. You can structure a custom query or a method that returns a Map in your Service or in additional method, but it’s not automatic — you have to be explicit about it….
Spring Boot RestController output fromat
Spring Boot decides which format (JSON, XML, Protobuf, etc.) to use when handling a request based on the configuration for content negotiation. 1. Content Negotiation in Spring Boot Spring Boot decides how to serialize and deserialize request/response bodies using HttpMessageConverters. These converters are automatically registered based on the libraries in the classpath. 2. How Does…
How to Optimize an Inefficient N+1 Query Problem in Spring Boot
The N+1 query problem is a common issue that occurs when using JPA repositories in Spring Boot. It happens when you have a one-to-many or many-to-one relationship between entities, and you’re fetching the parent entity with its children. This results in a separate database query for each child, leading to inefficient performance. Optimizing N+1 Query…
Object Mapping – examples With Java Spring Boot
There are several Java Frameworks available that does Object Mapping – automatic creation of DTO (Data Transfer Object) in Spring Boot. Reasons we need it Spring (Boot) Framework saves a lot of time of developers by hiding complexity and placing smart defaults with convention over configuration. This comes with the price of a “magic” happening…
Database Integrations in Spring Boot
Here are minimal examples of different Database Integrations in Spring Boot that you could choose from in your applications. 1. JdbcTemplate (Plain JDBC) JdbcTemplate is a lower-level approach that directly executes SQL queries. import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @Repository public class UserJdbcRepository { private final JdbcTemplate jdbcTemplate; public…
How to Update a Table using Sping Repository Modifying Query
You can use Spring Data JPA’s @ Modifying and @ Query annotations to achieve Update a field of a Table using Sping Repository Modifying Query. Spring Repository Here is an example: import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional; public interface UserRequestRepository extends CrudRepository<UserRequest, Long> { @Modifying @Query(“UPDATE UserRequest u SET u.fieldCount = 0”) void…
How to check every request in spring boot & if it is invalid – return 401
How to check for presence of specific header in every request with spring boot and if it is not – return 401? You can achieve this by implementing a custom HandlerInterceptor in Spring Boot. Here’s an example: import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; //import javax.servlet.http.HttpServletRequest; //import javax.servlet.http.HttpServletResponse; import java.util.Arrays; @Component public…
IOException: Stream Closed – when having ResponseEntity – a file
The error java.io.IOException: Stream Closed indicates that the InputStream passed to the InputStreamResource has already been closed before it is read by the Spring framework to create the response. Use Case The use case where you may need to encounter this error is file download from Spring Boot App. Writing down bytes loaded in memory…
How to implement partial commits in a Spring Boot service when using spring repositories
Implementing checkpoint or partial commits within a database transaction in a Spring Boot service can be tricky since Spring transactions are designed to be atomic: they either fully commit or fully roll back. However, you can achieve a similar effect by managing transactions explicitly in your service layer and leveraging Spring’s PlatformTransactionManager or nested transactions…