I'm new to Spring JPA. I has two questions about Example and ExampleMatcher API.
null
value. It is quite annoying to set all path names like below:ExampleMatcher<Product> matcher =ExampleMatcher.matching().ignorePaths("field_a", "field_b");
@ManyToOne
relation. User entity has several fields but my Example object has User field only filled with userId field. In this case I want to find product data which has user_id
foreign key column matching userId field value included in user object included in product Example object.Sorry for poor English... Actually this is my first question at Stack Overflow. Thanks for attention. I'm looking forward for great answers.
Spring Data by default will ignore null values in properties. So you need not ignore paths for null values. We could also use the withIgnoreNullValues()
(docs) method call on the matcher to explicitly tell it to ignore null values.
Note primitive values(int,double,etc) if not set will still be used since primitives can't have nulls and use default values instead so you should ignore the primitive properties if not used for matching.
For your second question, you could do something like the below
Product product = new Product();
User user = new User();
user.setId(5); // Id to be matched
product.setUser(user); // Associate User object with Product
Example<Product> example = Example.of(product,matcher);