如何让 Spring Data Couchbase 保留 id 字段?

How can I make Spring Data Couchbase persist the id field?

我刚刚开始一个使用 CouchBase 4.5 和 Spring Data CouchBase 的新项目。我有一个看起来像这样的实体:

@Document
public class ItemType implements Serializable {
    private static final long serialVersionUID = -874553420214538944L;

    @Id
    @Field
    private String key;

    @Field
    private String displayName;

    @Field
    private int appAccountId;

    @Field
    private Map<String, FieldType> fieldTypes;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public int getAppAccountId() {
        return appAccountId;
    }

    public void setAppAccountId(int appAccountId) {
        this.appAccountId = appAccountId;
    }

    public Map<String, FieldType> getFieldTypes() {
        return fieldTypes;
    }

    public void setFieldTypes(Map<String, FieldType> fieldTypes) {
        this.fieldTypes = fieldTypes;
    }
}

当我使用 CouchBase CrudRepository 保存它时,它在数据库中的结果如下:

{
  "_class": "com.mycompany.models.ItemType",
  "appAccountId": 2,
  "displayName": "Image - External",
  "fieldTypes": {
    "location": {
      "rank": 1,
      "type": "String",
      "publishCascade": false,
      "displayName": "Location",
      "displayAttributes": {
        "uiDisplayType": "TEXT_FIELD"
      }
    }
  }
}

一切似乎都很好,除了 "key" 字段没有保存在文档中。无论有没有 @Field 注释,我都试过了,但结果是一样的。有什么方法可以确保将 @Id 字段持久保存到数据库中?

您可能是在打保留字?

如果您将 "key" 更改为 "keyz" 之类的任何其他内容,它是否有效?

我确实看到 KEY 是 N1QL 中的保留字 -

http://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/reservedwords.html

Spring 数据 Couchbase 实现使用 @Id 字段作为 Couchbase 中的文档键。然后它会在持久性期间忽略该特定字段,并且 @Field 不会改变它。

在这种特殊情况下不忽略 @Field 会使反序列化逻辑复杂化,因为 1 个属性的值将在 2 个位置...