如何告诉 Spring Data Couchbase 嵌入或引用实体?

How to tell Spring Data Couchbase to embed or reference entity?

我的问题很简单:如何告诉 Spring Data Couchbase 使用 @Field 嵌入或引用实体?是否有额外的注释?

不确定您要的是哪个额外注释。

根据 documentation:

All entities should be annotated with the @Document annotation.

Also, every field in the entity should be annotated with the @Field annotation from the Couchbase SDK. While this is - strictly speaking - optional, it helps to reduce edge cases and clearly shows the intent and design of the entity. It can also be used to store the field under a different name.

There is also a special @Id annotation which needs to be always in place. Best practice is to also name the property id.

例如:

    import com.couchbase.client.java.repository.annotation.Id;
    import com.couchbase.client.java.repository.annotation.Field;
    import org.springframework.data.couchbase.core.mapping.Document;

    @Document
    public class User {

        @Id
        private String id;

        @Field
        private String firstname;

        @Field
        private String lastname;

        public User(String id, String firstname, String lastname) {
            this.id = id;
            this.firstname = firstname;
            this.lastname = lastname;
        }

        public String getId() {
            return id;
        }

        public String getFirstname() {
            return firstname;
        }

        public String getLastname() {
            return lastname;
        }
    }

话虽如此,文档中的另一个注释可能会有帮助:

If you want a different representation of the field name inside the document in contrast to the field name used in your entity, you can set a different name on the @Field annotation. For example if you want to keep your documents small you can set the firstname field to @Field("fname"). In the JSON document, you’ll see {"fname": ".."} instead of {"firstname": ".."}.

您不能使用@Field 引用其他文档,目前此注解的目的只是在最终JSON 文档中指定您的属性名称,但@Field 注解不是强制性的.

关于 ID 引用验证,添加这样的 feature/validation 有很多副作用,其中之一是您的写入吞吐量将受到显着影响。 MongoDB 有一个 Master/Slave 架构,这使得这个特性很容易实现,另一方面,它牺牲了可扩展性。

Couchbase 方法更倾向于依靠您的应用程序来进行此类验证(无论如何保存正确的数据是应用程序的责任)但要让您的 reads/writes 尽可能快。

我的个人观点:这种验证只是一个 RDBMS "bureaucracy",因为您的应用程序已经验证了所有内容。