Sunday, January 20, 2019

Spring Annotations

Spring Annotations

Annotations Description
@Component, @Repository and @Service @Component annotation is the more generalized form that are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. This annotation extended to more specific forms such as @Controller, @Repository and @Service.
@Autowired Spring bean dependencies are defined in the XML files, the same can be automatically detected by the Spring container by using the @Autowired annotation. This would eliminate using the XML configurations.
@Qualifier There may be scenarios when we create more than one bean of the same type and want to wire only one of them with a property. This can be controlled using @Qualifier annotation along with the @Autowired annotation.
@Required @Required annotation applies to bean property setter methods and enforces required properties
Difference between @Resource, @Autowired and @Inject This tutorial explains the difference between these three annotations @Resource, @Autowired and @Inject used for injecting the objects.
@Inject and @Named @Inject and @Named annotations are JSR-330 annotations was introduced in Spring 3 as alternative for the spring annotations @Autowired and @Component.
@Resource, @PostConstruct and @PreDestroy This tutorial explains the JSR-250 annotations that are introduced in Spring 2.5, which include @Resource, @PostConstruct and @PreDestroy annotations.
@RestController @RestController annotation is inherited from the @Controller annotation. This annotation is the special version of @Controller annotation for implementing the RESTful Web Services. @RestController has been added as part of the Spring 4 release.
@RequestHeader @RequestHeader annotation for facilitating us to get the header details easily in our controller class. This annotation would bind the header details with the method arguments, and it can be used inside the methods.
@ControllerAdvice @ControllerAdvice annotation used for defining the exception handler with specific exception details for each method If any exception thrown on any part of the application will be handled by this component.
@ModelAttribute @ModelAttribute annotation can be used as the method arguments or before the method declaration. The primary objective of this annotation to bind the request parameters or form fields to an model object.
@Conditional This tutorial explains one of the new features introduced in spring 4, conditional annotation type.
@Query This spring data series tutorial explains @Query annotation and how to create custom query using the @Query annotation.
@Profile This tutorial explain how to enable profiles in your spring application for different environments.
@Configuration Instead of using the XML files, we can use plain Java classes to annotate the configurations by using the @Configuration annotation. If you annotate a class with @Configuration annotation, it indicates that the class is used for defining the beans using the @Bean annotation.
@PathVariable We have to use @PathVariable for accepting the customized or more dynamic parameters in the request paths.
@Controller, @RequestMapping, @RequestParam, @SessionAttributes and @InitBinder This tutorial explains you some of the key annotations that are related to Spring MVC applications @Controller, @RequestMapping, @RequestParam, @SessionAttributes and @InitBinder
Difference between @RequestParam and @PathVariable This tutorial explains you what is the difference between the two annotations @RequestParam and @PathVariable.
@RequestMapping @RequestMapping annotation is used for mapping web requests to a particular handler classes or handler methods.
@Value  This tutorial shows how to load the properties file values using the @Value annotation.
@Import @Import is the annotation used for consolidating all the configurations defined in various configuration files using @Configuration annotation.
@Transactional Use annotation @Transactional in order to define a particular method that should be within a transaction.
@SpringBootApplication This is annotation is the heart of spring boot application. @SpringBootApplication indicates that it is the entry point for the spring boot application.
@EnableAutoConfiguration This example demonstrates how to use the @EnableAutoConfiguration annotations for auto-configuring the spring boot applications.
@EnableCaching @EnableCaching annotation is the annotation-driven cache management feature in the spring framework. This annotation has been added to the spring since the version 3.1.

Spring MVC Request Processing


 request lifecycle
  1. DispatcherServlet receives the request.
  2. DispatcherServlet dispatches the task of selecting an appropriate controller to HandlerMapping. HandlerMapping selects the controller which is mapped to the incoming request URL and returns the (selected Handler) and Controller to DispatcherServlet.
  3. DispatcherServlet dispatches the task of executing of business logic of Controller to HandlerAdapter.
  4. HandlerAdapter calls the business logic process of Controller.
  5. Controller executes the business logic, sets the processing result in Model and returns the logical name of view to HandlerAdapter.
  6. DispatcherServlet dispatches the task of resolving the View corresponding to the View name to ViewResolver. ViewResolver returns the View mapped to View name.
  7. DispatcherServlet dispatches the rendering process to returned View.
  8. View renders Model data and returns the response.

Spring mvc HandlerMapping VS HandlerAdapter

Sol :

  1. DispatcherServlet dispatches the task of executing of business logic of Controller to HandlerAdapter.
  2. HandlerAdapter calls the business logic process of Controller.
  3. Controller executes the business logic, sets the processing result in Model and returns the logical name of view to HandlerAdapter.

Sunday, January 13, 2019

Difference Between Merge And Update Methods In Hibernate?

  • update() and merge() methods in hibernate are used to convert the object which is in detached state into persistence state.[  But there are different situation where we should be used update() and where should be used merge() method in hibernate  ]
  •  Example:
Employee emp1 = new Employee();
emp1.setEmpId(100);
emp1.setEmpName("Dinesh");
//create session
Session session1 = createNewHibernateSession();
session1.saveOrUpdate(emp1);
session1.close();
//emp1 object in detached state now

emp1.setEmpName("Dinesh Rajput");//Updated Name
//Create session again
Session session2 = createNewHibernateSession();
Employee emp2 =(Employee)session2.get(Employee.class, 100);
//emp2 object in persistent state with id 100

//below we try to make on detached object with id 100 to persistent state by 
using update method of hibernate
session2.update(emp1);//It occurs the exception NonUniqueObjectException 
because emp2 object is having employee with same empid as 100. In order 
//to avoid this exception we are using merge like given below instead of
 session.update(emp1);

session2.merge(emp1); //it merge the object state with emp2
session2.update(emp1); //Now it will work with exception
 

What's the advantage of persist() vs save() in Hibernate?

persist() is well defined. It makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. The spec doesn't say that, which is the problem I have with persist().
persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.
A method like persist() is required.
save() does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context.

Saturday, January 12, 2019

Hibernate IDENTITY vs SEQUENCE entity identifier generators?

  • The IDENTITY generator will always require a database hit for fetching the primary key value without waiting for the flush to synchronize the current entity state transitions with the database.
  •  IDENTITY generator doesn't play well with Hibernate write-behind first level cache strategy.
  • Insert a row without specifying a value for the ID. After inserting the row, ask the database for the last generated ID.
  • The GenerationType.IDENTITY is the easiest to use but not the best one from a performance point of view.

How Does Spring @Transactional Really Work?

JPA on itself does not provide any type of declarative transaction management. When using JPA outside of a dependency injection container, transactions need to be handled programmatically by the developer

Spring Annotations