是否有任何可能的方法可以忽略JPA示例匹配器的所有路径 [英] Are there any possible ways to ignore all paths of JPA Example Matcher

查看:134
本文介绍了是否有任何可能的方法可以忽略JPA示例匹配器的所有路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring JPA的新手. 我对Example和ExampleMatcher API有两个疑问.

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

  1. 除了设置匹配器的某些路径之外,是否有任何其他方法可以忽略所有路径.或者,如果Example对象的路径具有null值,则有任何方法可以忽略所有路径.像下面这样设置所有路径名是很烦人的:
  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. 如何使用Example匹配连接的列.例如.产品实体的用户实体"字段为@ManyToOne关系.用户实体具有多个字段,但是我的Example对象的User字段仅填充了userId字段.在这种情况下,我要查找产品数据,该产品数据的user_id外键列与产品Example对象中包含的用户对象中包含的userId字段值匹配.
  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.

对不起,英语不好……实际上,这是我在Stack Overflow上遇到的第一个问题. 感谢您的关注. 我期待着很好的答案.

Sorry for poor English... Actually this is my first question at Stack Overflow. Thanks for attention. I'm looking forward for great answers.

推荐答案

Spring数据 默认情况下 将忽略属性中的空值.因此,您不必忽略null值的路径.我们还可以使用withIgnoreNullValues()(

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.

请注意,如果未设置原始值(int,double等),则仍将使用,因为原始值不能为null,而使用默认值,因此,如果不用于匹配,则应忽略原始值.

对于第二个问题,您可以执行以下操作

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

这篇关于是否有任何可能的方法可以忽略JPA示例匹配器的所有路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆