Spring data mongodb 只读属性键已从数据库中删除

问题描述 投票:0回答:1

我用 @ReadOnlyProperty 注释了实体 pojo 中的一些键,因为我不想从工作应用程序中设置这些字段,因为这些字段将从不同的应用程序中设置/更新。

@Document
public class SimpleClass{

    @Id
    @Field(targetType = FieldType.OBJECT_ID)
    private String id;
    
    // this key will be updated from current application
    private String key1;

    /// below key will be created/updated from different flow
    /// I don't want to create/update this data from current application
    /// I want this key for read purpose only
    @ReadOnlyProperty
    private String key2;


    //SETTERS - GETTERS Ommited
}

我从 pojo 创建了 mongo 存储库。

public interface SimpleClassRepository extends MongoRepository<SimpleClass, String>{

}

我正在尝试通过 simpleClassRepository.save() 方法更新已读取的文档。 数据库文档正在更新其他字段,但这些 @ReadOnlyProperty 键已从文档中删除。 我正在使用 spring data mongodb 3.4.1。 谁能建议如何解决这个问题

我尝试也在实体中添加@Version,但结果仍然相同。

spring mongodb spring-data-jpa spring-data spring-data-mongodb
1个回答
0
投票

save
方法将用存在的值覆盖任何现有文档。如果您需要保留某些值,请通过 templaterepository API 发布更新。

© www.soinside.com 2019 - 2024. All rights reserved.