簡體   English   中英

Spring MongoDB 查找集注釋的 id 字段為 null

[英]Spring MongoDB find sets annotated id field as null

我正在使用spring-data-mongodb將我的 Java 對象持久化到 MongoDB。 除了一項特定操作外,一切正常:

 @Override
 public Collection<MyDocument> findAllByTags(Collection<String> tags) {
        FindIterable<MyDocument> results = operations.getCollection(COLLECTION_NAME)
                .find(Filters.all(FIELD_TAGS, tags), MyDocument.class);
        return StreamSupport.stream(results.spliterator(), false).collect(Collectors.toList());
 }

文檔 class 如下所示:

@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Document
public class MyDocument implements MyEntity {


    @Id
    @EqualsAndHashCode.Include
    @BsonProperty("myId")
    private String myId;

    @BsonProperty("dateCreated")
    private Date dateCreated;

    @BsonProperty("otherField")
    private String otherField;

    @Indexed
    @BsonProperty("tags")
    private Collection<String> tags;

    //more fields

所有對象的所有字段都按預期返回,但使用@Id注釋的字段設置為null 有誰知道是什么導致了這種行為以及如何解決它? 感謝您的時間。

我得到它的工作如下:

 @Getter
 @Setter
 @NoArgsConstructor
 @EqualsAndHashCode(onlyExplicitlyIncluded = true)
 @Document
 public class MyDocument implements MyEntity {
    
    
    @MongoId(value = FieldType.STRING)
    private String myId;

   //... 

和查詢:

 @Override
 public Collection<MyDocument> findAllByTags(Collection<String> tags) {
     return operations.find(query(Criteria.where(FIELD_TAGS).all(tags)), MyDocument.class);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM