2

I would like to know how to generate a unique id with prefix e.g. user::524525 with Couchbase SDK.

When I started with Couchbase using the Couchbase JavaSDK guide, I noticed that in all the examples the id looks like TYPE::ID e.g user::king_arthur. This is also recommended to avoid conflicting ids for different documents. As I was reading the documentation from Spring Data Couchbase I thought the way to do it would be

import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy;
import org.springframework.data.couchbase.core.mapping.id.IdPrefix;
import com.couchbase.client.java.repository.annotation.Id;
...

@Data
public class UserĀ {    

    @IdPrefix
    private String prefix = "user";

    @Id @GeneratedValue(delimiter = "::", strategy = GenerationStrategy.USE_ATTRIBUTES)
    private String id;
}

but when I test this and check in the db, the ID is just "user".

Am I missing something? Is it a bug? I would appreciate any suggestions.

LazR
  • 631
  • 8
  • 18

1 Answers1

3

So actually I found the answer, I had to use GenerationStrategy.UNIQUE like this:

@IdPrefix
private String prefix = "user";

@Id @GeneratedValue( delimiter = "::", strategy = GenerationStrategy.UNIQUE)
private String id;

which generates a unique id like user::8a9gfa4d55f7.

The JavaDoc is confusing though, because for GenerationStrategy.USE_ATTRIBUTES it explicitly says it will use the prefix:

Constructs key from the entity attributes using the supplied id, prefix, suffix

but not for GenerationStrategy.UNIQUE:

Uses an uuid generator

LazR
  • 631
  • 8
  • 18