Difference between Spring Data JPA and JPA

JPA (Java Persistence API) What is JPA? JPA, the Java Persistence API, is the standard specification for object-relational mapping (ORM) in Java. ORM allows automatic mapping between an object and a relational database. JPA is a collection of interfaces used for ORM. With JPA, developers can develop using object-oriented programming, and JPA will automatically generate the appropriate SQL for the relational database and execute it. Features of JPA Standard specifications: JPA is the standard specification for implementing ORM in Java. Object-oriented development: With JPA, developers can write code using an object-oriented approach. Automatic SQL generation: JPA automatically generates and executes SQL without requiring developers to write SQL statements. Implementation required: JPA is an interface, so it requires an implementation. Spring Data JPA What is Spring Data JPA? Spring Data JPA is a technology from Spring that makes it easier to use JPA. Using JPA requires many settings, such as EntityManagerFactory, EntityManager, and EntityTransaction, but Spring Data JPA handles these settings. Spring Data JPA also provides additional features to JPA. ...

June 7, 2024 · 2 min · 334 words · In-Jun Hwang

Exploring the Dispatcher Servlet

What is the Dispatcher Servlet? The Dispatcher Servlet is at the heart of Spring MVC. It is responsible for receiving client requests, delegating them to the appropriate controller, and then passing the result returned by the controller to the view. There is only one Dispatcher Servlet in a web application, and it handles all client requests. How the Dispatcher Servlet Works Receives a request from the client. Finds the controller that will handle the client’s request through Handler Mapping. Executes the controller through Handler Adapter. Transforms the result returned by the controller into a View through View Resolver. Forwards the view to the client. When using RestController, View Resolver is not used. Instead, the object is converted to JSON format and sent to the client. ...

June 5, 2024 · 3 min · 434 words · In-Jun Hwang

What is Spring Interceptor?

Spring Interceptor Interceptor performs similar role to Servlet Filter. Servlet filter provides functionality to intercept and process requests before and after they reach the servlet container. Spring Interceptor provides functionality to intercept and process requests before and after they reach the controller in Spring MVC. Interceptor is implemented using HandlerInterceptor interface. HandlerInterceptor interface provides three methods: preHandle: Method executed before the request reaches the controller postHandle: Method executed after the request has reached the controller, before the view is rendered afterCompletion: Method executed after the view has been rendered Interceptor Implementation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CustomInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // Code to execute before the request reaches the controller // If false is returned, request is not forwarded to the controller return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // Code to execute after the controller is executed normally // Not executed if an exception is thrown } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // Code to execute after the view has been sent to the client // Exception object can be used to check exception information if an exception occurred // Exception information can be checked and logged } } Interceptor Registration To register the interceptor, implement the WebMvcConfigurer interface and override the addInterceptors method. ...

June 4, 2024 · 3 min · 567 words · In-Jun Hwang

Getting to know Spring Boot

What is Spring? Spring is a framework based on the Java language that provides various features for developing enterprise-grade applications. Spring has the following characteristics: Lightweight container: Spring is a lightweight container that manages the creation of objects, their lifecycle, and dependency management. Inversion of Control (IoC): Spring supports Inversion of Control, which allows the Spring container to manage the creation, lifecycle, and dependency management of objects. Dependency Injection (DI): Spring supports Dependency Injection, which allows the Spring container to manage the dependencies between objects. ...

May 16, 2024 · 5 min · 1030 words · In-Jun Hwang