Skip to main content

Posts

Showing posts from May, 2018

Spring Data JPA Paging, Sorting, Custom Repository, Auditing and Locking

In this module we will talk about some of advance features that spring data JPA support for developing an enterprise application. They are paging and sorting ,auditing, locking and custom repository. Paging and Sorting As a web application developer if you have to display thousands of records on the UI it is not a good practice to just query database and display it to UI. Spring data JPA provides paging and sorting mechanism to handle such type of scenario. These are most sought problem if you are dealing with a big enterprise application, keeping this into mind spring data JPA has provided in built support for paging and sorting. You can see above that spring data JPA has PagingAndSortingRepository interface and it has findAll method. Sort and Pageable object are getting passed as part of findAll() method parameters. What do you think Pageable does behind the scene. Basically Pageable is an interface and when spring data JPA fetch value from database it limits the result. So inte

Spring Data JPA and @Query Annotation

As in the last post on query DSL, I have discussed about creating various custom finder methods to query in database. You also know that spring data JPA internally form JPQL to query in database. In this module I am going to cover query annotations that spring data JPA provides with some advanced features. @Query annotation In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. In the custom JPA repository we can define multiple query DSL methods to query data from database for different condition. Consider a case where condition being passed in the query are too many. Though you can form custom finder method but the method is going to be very large and cumbersome. In such type of scenario the best way is to write simple method and use @Query annotation on top of method. When the @query annotation is being used spring data JPA ignores the structure of f

Query DSL Overview

This post is the continuation of my previous post on spring data JPA. In this post I would be delving deeply into query DSL.Query DSL is an advanced feature provided by spring data JPA to query data from database. It has less code so less to maintain.By using query DSL one can check query at start up rather than at runtime. I am going to discuss key and concepts require to learn DSL and would learn through example. We will extending the same promo-api that we wrote in last post and would be writing Junit to verify query. DSL stands for a domain specific language and is a customized extension of a software programming language that addresses a specific business or domain. In case of spring data JPA this means the framework is enhancing Java to be better suited for creating and working with JPA query. The spring data JPA query DSL is simply all about finding terms and syntax to work with JPA query more efficiently. To demonstrate how query DSL works ,let us take an example to explain

Getting Started With Spring Data JPA

Spring data JPA is the most sought framework for connecting spring application to the database.It is a very vast topic ,I will try to cover from basic to intermediate level.Our goal is to how to write our persistence layer codebase in spring application using spring data JPA. I will first covering the bit of theory part then I would be giving an example to demonstrate these concept. Before getting deep dive into Spring data JPA ,look at below code for performing basic CRUD operation in database using JPA entity manager. As you can see above the amount of code needed to perform crud operation in database.This is standard code that one's follow if you are going to build persistence tier using JPA.I am not going to explain the details of JPA here as my more focus is to demonstrate spring data JPA.JPA stands for Java persistence API ,it is an API for doing transaction in database. The same operation you can perform in spring data JPA by writing minimal code.Loot at below example f

Testing the Spring Boot App

So far we have covered the fundamentals of spring boot and spring boot persistence layer.This is continuation of last two post on spring boot.If you have not gone through my previous post ,I would recommend you to go through my previous post. In this session we would be getting familiar with testing framework and how spring boot can be integrated by test framework.Over the years in almost every industry test driven development become very essential in development cycle.As most company now a days prefer to first write unit testing then go over development.While learning spring boot it it essential to know about test driven development in spring boot as in spring boot makes testing easier than ever. Getting started with Spring Boot Testing When it comes to testing there are several different ways that you can write unit test to your application.Before doing any testing first we need to integrate testing framework, Spring boot provides spring-boot-starter-test to add dependency for

Configuring and Accessing Data Source in Spring Boot

In the last session we have learned everything about spring boot ,we have also created spring boot rest controller with basic CRUD operation. In this post we are going to learn about how to integrate spring boot application through data base.In this lesson we will be using spring data JPA to connect data source with spring boot application and flywayDB for data migration. Identifying framework for Integration To integration spring boot application with data source we need to understand how our persistence layer would work and what are frameworks needed to developed full functional spring boot application.Since we are talking about persistence we are going to focus on server side part of our spring boot application. Currently we have rest API set up for controller under spring MVC framework and this sits at our top of server side stack. We are going to use spring data JPA to connect to data source and embedded H2 data base for persistence layer.To integrate spring data JPA we jus