I have 2 entities: field and crop. Field looks like this:
public class Field {
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name = "crop")
private Crop crop;
}
public class Crop {
private Integer id;
private String name;
}
I want to query by example the exact field I get in a request.
public class FieldRequest {
String name;
Integer cropId;
}
My service looks like this:
public Optional<Field> getField(FieldRequest request) {
Field fieldExample = Field.builder()
.name(request.name)
.crop(Crop.builder()
.id(request.cropId)
.build())
.build();
fieldRepository.findOne(Example.of(fieldExample, ExampleMatcher.matching()
.withIncludeNullValues()
.withIgnorePaths("id")
.withMatcher("crop.id", ExampleMatcher.GenericPropertyMatcher::exact
);
}
my problem is that the query that being created ignores the crop and returns the wrong field. How can I make the query to search by the crop id (I don't care about the crop name), I tried with and without the "Crop.id" matcher.