简体   繁体   English

Spring数据mongodb存储库findAll字段排除

[英]Spring data mongodb repository findAll field exclusion

I have a strange issue for the spring data mongodb repositories.. I want to exclude a field from my findAll request. 我对spring数据mongodb存储库有一个奇怪的问题..我想从findAll请求中排除一个字段。 How can I achieve this ? 我怎样才能做到这一点?

This works perfectly: 这非常有效:

@Query(fields = "{'objectContentAsJson':0}")
Page<ObjectHistory> findByObjectIdAndServiceIgnoreCase( String objectId, String service, Pageable pageable );

But no chance for findAll : 但是,没有机会findAll

@Query(fields = "{'objectContentAsJson':0}")
Page<ObjectHistory> findAll( Pageable pageable );

This throws: 抛出:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type ObjectHistory! 引起:org.springframework.data.mapping.PropertyReferenceException:找不到类型为ObjectHistory的属性findAll! at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241) at org.springframework.data.repository.query.parser.Part.(Part.java:76) org.springframework.data.mapping.PropertyPath。(PropertyPath.java:75)位于org.springframework.data.mapping.PropertyPath.create的org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) PropertyPath.java:307)org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)atg.springframework.data .repository.query.parser.Part。(Part.java:76)

Adding an empty filter criteria will do the trick for you: 添加一个空的过滤条件将为您解决问题:

@Query(value = "{}", fields = "{'objectContentAsJson':0}")
Page<ObjectHistory> findAll(Pageable pageable);

Apparently, when you do not specify the value parameter to filter results, Spring Data tries to derive a query from the method name and somehow, does not recognize the special meaning of findAll . 显然,当您没有指定value参数来过滤结果时,Spring Data会尝试从方法名称派生查询,并且不知何故,不会识别findAll的特殊含义。

Since you are not providing a value field to your @Query annotation, Spring will try to convert the method name findAll to a query which is not respecting the Query Creation Specification from Spring. 由于您没有为@Query注释提供字段,因此Spring将尝试将方法名称findAll转换为不遵循Spring的查询创建规范的查询。 Please look a the Specs here . 请看这里的规格。

This should work for you : 这应该适合你:

@Query(value = "{}", fields = "{'objectContentAsJson':0}")
Page<ObjectHistory> findAll(Pageable pageable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM