Why use SpecificationBuilder ?

Spring boot Specification Builder

ยท

2 min read

Why use SpecificationBuilder ?

Hi there ,lets discuss it.

Often in spring boot application we receive many api request which requires some filter on data .These Filter values are sent From Frontend in payload body.

Also what if there are other request which require different filter on same data. Then will you make another query to tackle this code situation?

If you ask that's quite inefficient. So to keep the code more efficient and great we use specification builder that helps us in making this query using java code in more generic and dynamic way.

To Understand it more better way first lets check conventional way below

For example: We are receiving a request like below

Which is having below data in body.--

    {
        "name":"username",
        "Rollno":"63728292"
    }

We will write a query like below in our server side in java code:

Select * from table where name ='' and rollno ='';

And another request is coming which is having below request body--

{
  "name":"username",
  "Rollno":"63728292",
  "Fathername":"daddy"
}

Now a new query will be created for this .

but when user is will have a filter in frontend of more than 20 fields and its varying accordingly then he will try to add more conditions for every filter fields if to add in query string if its not null. And after all changes when we check code that look inefficient.

So in here if we will use Specification builder to make queries of where clause then that code will give us Senior code level developer vibe and efficient approach. Plz note i have replaced cb.equal(root.get("class field name ", fieldValue) Check below Code Snippet:

import com.solucionis.porta.specifications.SpecificationBuilder;
import org.springframework.data.jpa.domain.Specification;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import org.slf4j.Logger;
import static com.solucionis.porta.specifications.SpecificationBuilder.isEqual;
import java.util.List;

public class TestingSpecificationBuilder {

  private TestingSpecificationBuilder() {
  throw new IllegalStateException("TestingSpecificationBuilder class");
  }

  public static Specification<TestingClass> searchSpecification(TestingClass search) {

  List<Specification<TestingClass>> searchSpecification = new ArrayList<>();

  if (StringUtils.isNotBlank(search.getUserId())) {
    searchSpecification.add(SpecificationBuilder.isEqual("userId", search.getUserId()));
  }
  if (CommonUtil.isNotEmpty(search.getRelationId())) {
    searchSpecification.add(SpecificationBuilder.isEqual("relationId", search.getRelationId()));
  }
  if (CommonUtil.isNotEmpty(search.getAccountIds())) {
    searchSpecification.add(SpecificationBuilder.isFieldIn("accountNumber", search.getAccountIds()));
  }
  if (CommonUtil.isNotEmpty(search.getAccountId())) {
    searchSpecification.add(SpecificationBuilder.isEqual("accountNumber", search.getAccountId()));
  }
  if (CommonUtil.isNotEmpty(search.getAccountName())) {
    searchSpecification.add(SpecificationBuilder.isLike("accountname", search.getAccountName()));
  }

  return SpecificationBuilder.combineWithAnd(searchSpecification);
  }

}

As you can see in this code we have added all our fields conditions and added conditions to match data from db using entity class.

After this just use it in your criteria builder to make predicates and java and spring boot will do its magic to create a query.

Hope you liked it if you need more details of it just comment i will update it with more details

ย