For the latest stable version, please use Spring Data Couchbase 5.4.0!spring-doc.cn

Auto generating keys

This chapter describes how couchbase document keys can be auto-generated using builtin mechanisms. There are two types of auto-generation strategies supported.spring-doc.cn

The maximum key length supported by couchbase is 250 bytes.

Configuration

Keys to be auto-generated should be annotated with @GeneratedValue. The default strategy is USE_ATTRIBUTES. Prefix and suffix for the key can be provided as part of the entity itself, these values are not persisted, they are only used for key generation. The prefixes and suffixes are ordered using the order value. The default order is 0, multiple prefixes without order will overwrite the previous. If a value for id is already available, auto-generation will be skipped. The delimiter for concatenation can be provided using delimiter, the default delimiter is ..spring-doc.cn

Example 1. Annotation for GeneratedValue
@Document
public class User {
     @Id @GeneratedValue(strategy = USE_ATTRIBUTES, delimiter = ".")
     private String id;
     @IdPrefix(order=0)
     private String userPrefix;
     @IdSuffix(order=0)
     private String userSuffix;
     ...
}

Key generation using attributes

It is a common practice to generate keys using a combination of the document attributes. Key generation using attributes concatenates all the attribute values annotated with IdAttribute, based on the ordering provided similar to prefixes and suffixes.spring-doc.cn

Example 2. Annotation for IdAttribute
@Document
public class User {
     @Id @GeneratedValue(strategy = USE_ATTRIBUTES)
     private String id;
     @IdAttribute
     private String userid;
     ...
}

Key generation using uuid

This auto-generation uses UUID random generator to generate document keys consuming 16 bytes of key space. This mechanism is only recommended for test scaffolding.spring-doc.cn

Example 3. Annotation for Unique key generation
@Document
public class User {
     @Id @GeneratedValue(strategy = UNIQUE)
     private String id;
     ...
}