Skip to content
Java Annotation Processor for Spring
Java Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows
src
.gitignore
README.md
compile.sh
pom.xml

README.md

kolobok

Java Annotation Processor for Spring. It contains a set of annotations that makes working with Spring easier. It is inspired by Lombok.

@FindWithOptionalParams

Usually Spring Data Repository interface includes find methods. Something like

@Repository
public interface PersonRepo extends PagingAndSortingRepository<Person, Long> {
    Iterable<Person> findByFirstNameAndLastNameAndCityId(String firstName, String lastName, Long cityId);
}

But if you want to find only by cityId and lastName (without specifying firstName) you need to add new method

    Iterable<Person> findByLastNameAndCityId(String lastName, Long cityId);

@FindWithOptionalParams annotation allows you to use original findByFirstNameAndLastNameAndCityId method with null values for params which should not be used in search criteria.

@Repository
public interface PersonRepo extends PagingAndSortingRepository<Person, Long> {
    @FindWithOptionalParams
    Iterable<Person> findByFirstNameAndLastNameAndCityId(String firstName, String lastName, Long cityId);
}

So now you can call

    Iterable<Person> persons = personRepo.findByFirstNameAndLastNameAndCityId(null, "Smith", 1L);

How it works?

It generates all possible find methods for search params.

  Iterable<Person> cityId(Long cityId);
  Iterable<Person> lastName(String lastName);
  Iterable<Person> lastNameAndCityId(String lastName, Long cityId);
  Iterable<Person> firstName(String firstName);
  Iterable<Person> firstNameAndCityId(String firstName, Long cityId);
  Iterable<Person> firstNameAndLastName(String firstName, String lastName);
  Iterable<Person> firstNameAndLastNameAndCityId(String firstName, String lastName, Long cityId);

Also it generates default implementation of findByFirstNameAndLastNameAndCityId method that checks which params are null and calls corresponded method.

    default Iterable<Person> findByFirstNameAndLastNameAndCityId(String firstName, String lastName, Long cityId) {
      if(firstName == null) {
        if(lastName == null) {
          if(cityId == null) {
            return findAll();
          } else {
            return cityId(cityId);
          }
        } else {
          if(cityId == null) {
            return lastName(lastName);
          } else {
            return lastNameAndCityId(lastName, cityId);
          }
        }
      } else {
        if(lastName == null) {
          if(cityId == null) {
            return firstName(firstName);
          } else {
            return firstNameAndCityId(firstName, cityId);
          }
        } else {
          if(cityId == null) {
            return firstNameAndLastName(firstName, lastName);
          } else {
            return firstNameAndLastNameAndCityId(firstName, lastName, cityId);
          }
        }
      }
    }

How to use it?

Just include kolobok library in your project. Maven:

  <dependency>
    <groupId>com.github.ukman</groupId>
    <artifactId>kolobok</artifactId>
    <version>0.1.2</version>
    <scope>compile</scope>  
  </dependency>

Gradle:

compileOnly 'com.github.ukman:kolobok:0.1.2'

And mark find methods with new annotations.

You can’t perform that action at this time.