问题描述
我是Spring JPA的新手.我对Example和ExampleMatcher API有两个疑问.
I'm new to Spring JPA.I has two questions about Example and ExampleMatcher API.
- 除了设置匹配器的某些路径之外,是否有任何其他方法可以忽略所有路径.或者,如果Example对象的路径具有
null
值,则有任何方法可以忽略所有路径.像下面这样设置所有路径名是很烦人的:
- 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");
- 如何使用Example匹配连接的列.例如.产品实体的用户实体"字段为
@ManyToOne
关系.用户实体具有多个字段,但是我的Example对象的User字段仅填充了userId字段.在这种情况下,我要查找产品数据,该产品数据的user_id
外键列与产品Example对象中包含的用户对象中包含的userId字段值匹配.
- 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 hasuser_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()
( docs )方法在匹配器上调用,以明确告诉它忽略空值.
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示例匹配器的所有路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!