Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExampleMatcher.withIgnorePath is fetching property path that is ignored

I have an entity which has multiple property paths. I used Query By Example and ExampleMatcher to ignore some of the property path.

My entity looks like below, Employee.class

private Integer id;
private String name;
private String designation;

I wanted to show only the Name and Id from the entity(fields in the table). For that, I did the following,

ExampleMatcher exampleMatcher = ExampleMatcher.matching().withIgnoreNullValues().withIgnorePaths("designation");
Example<Employee> example = Example.of(entity, exampleMatcher);

But, the response returns all the values including the property given in the ignorePath.

Kindly assist me on how to ignore the property paths.

like image 816
r123 Avatar asked Jul 03 '18 11:07

r123


People also ask

What is ExampleMatcher?

A generic property matcher that specifies string matching and case sensitivity. static class. ExampleMatcher.GenericPropertyMatchers. Predefined property matchers to create a ExampleMatcher.

What is example in JPA?

Query by Example (QBE) is a method of query creation that allows us to execute queries based on an example entity instance. The fields of the entity instance need to be populated with the desired criteria values.


Video Answer


2 Answers

withIgnorePaths("designation") prevents query-by-example from applying a filter based on the example's designation value. It does not prevent designation from being populated in the query result.

If you want to exclude certain properties from the query result, use projections or query graphs (depending on which is a better fit for your particular use case). Not sure if there's a way they can be used with query-by-example, though.

like image 182
crizzis Avatar answered Oct 09 '22 23:10

crizzis


Am not sure if this will work

ExampleMatcher exampleMatcher = ExampleMatcher.matching().withIgnorePaths("designation").withIgnoreNullValues();

I just added withIgnorePaths first.

Refering to This link

like image 44
MyTwoCents Avatar answered Oct 09 '22 23:10

MyTwoCents