Couchbase document id generation

做~自己de王妃 提交于 2019-12-24 17:26:16

问题


I have an Springboot application integrated with couchbase 6.0. I have read that if a key is annotated with @Id then it will saved as an document id and will not be a part of the json.

Then i have used @Id and @Field together on the key but still that field is not appearing on json document.

Can somebody help me out on the following:

1: How to make same key as document id and keep that a part of json also.

2: If a field is declared with @Id, it is created as document id and does not appear on document, but when i do a get request same key appear on the response.

I have also tried, click here https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration.usingattributes

My entity is:

@Id @GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES,delimiter="::")
private String id;
@IdAttribute
private String userid;
@Field
private String fname;
@Field
private String lname;

Post method body:

{

"id": "888",
"userid":"user1",
"fname": "xyz",
"lname": "abc"

}

In Couchbase server, it is saving this document as

It is creating document id as 888 only, it is supposed to generate documnet id as 888::user1

I have tested this many times but the result is same.


回答1:


Looks like you're looking for 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.

So, for example you can do:

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

According to the example above you will get as a result a Document with the id = userId and also the field userId in the json.

UPDATE AS PER QUESTION EDIT

You may have to include a new filed in your entity, let's say postId and change the naming of the id field in your post method body by postId, something like:

Entity:

@Id @GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES,delimiter="::")
private String id;
@IdAttribute(order = 0)
private String postId;
@IdAttribute(order = 1)
private String userid;
@Field
private String fname;
@Field
private String lname;

Post method body:

{
"postId": "888",
"userid":"user1",
"fname": "xyz",
"lname": "abc"
}


来源:https://stackoverflow.com/questions/53607184/couchbase-document-id-generation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!