Are there any possible ways to ignore all paths of JPA Example Matcher

大憨熊 提交于 2019-12-10 21:18:22

问题


I'm new to Spring JPA. I has two questions about Example and ExampleMatcher API.

  1. Are there any ways to ignore all paths except some paths which I set matchers. Or are there any ways to ignore all paths if Example object's path has null value. It is quite annoying to set all path names like below:

ExampleMatcher<Product> matcher =ExampleMatcher.matching().ignorePaths("field_a", "field_b");

  1. How to match joined column using Example. For example. Product entity has User entity field as @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.


回答1:


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);


来源:https://stackoverflow.com/questions/45228206/are-there-any-possible-ways-to-ignore-all-paths-of-jpa-example-matcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!