4

我刚刚开始使用 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"
      }
    }
  }
}

一切似乎都很好,除了“关键”字段没有保留在文档中。我在有和没有 @Field 注释的情况下都试过了,但结果是一样的。有什么方法可以确保 @Id 字段持久保存到数据库中?

4

2 回答 2

0

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

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

于 2016-07-11T07:14:19.460 回答
-1

你可能打了一个保留字?

如果您将“key”更改为“keyz”之类的其他任何内容,它会起作用吗?

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

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

于 2016-07-08T19:11:17.447 回答