NotGaeL
NotGaeL

Reputation: 8484

Spring Boot JPA search matches for all non null values

Is there an easy way to populate some of the fields of an entity and tell spring to return all the entities in the repository matching all the non-null values?

Example:

Jacket jacket = new Jacket();
jacket.setColor("blue");
jacket.setSize("XL");
Pocket pocket= new Pocket();
pocket.setShape("round");
pocket.setType("open");
jacket.setPocket(pocket);
Set<Jacket> matchingJackets = jacketRepository.findAllLike(jacket);

Where Jacket and Pocket are @Entity and jacketRepository is the autogenerated repo for public interface JacketRepository extends org.springframework.data.repository.CrudRepository<Jacket, UUID>

(Take on account I am not looking for a solution to this specific case, but all cases, if for example I don't pocket.setShape("someshape"); it should return all matching jackets without looking at the pocket shape)

Upvotes: 1

Views: 1485

Answers (1)

mh-dev
mh-dev

Reputation: 5503

Use the Query by example feature of spring-data-jpa

See http://docs.spring.io/spring-data/jpa/docs/1.10.4.RELEASE/reference/html/#query-by-example

Upvotes: 2

Related Questions