Couchbase - 使用用户定义的后缀自动生成密钥

问题描述 投票:4回答:1

我想使用spring boot java在couchbase中保存一个employee对象。我正在使用反应式couchbase驱动程序。我的要求是使用带有硬编码字符串“-EMPLOYEETYPE”的employeeId保存employee对象。

示例:来自Java Application的对象到couchbase:

{ "employeeId" : "12345", "lname" :"ltest", "fname" : "ftest"}

在保存到沙发基座的同时,应该生成关键字

"12345-EMPLOYEETYPE"

下面的代码不起作用,请指导我如何实现它。注意:我使用的是lombok所以没有吸气剂和二传手。

@Document
public final class Employee {

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

        @IdAttribute
        private String employeeId;
}
java spring-boot couchbase spring-webflux
1个回答
2
投票

找到了解决方案。我们需要创建一个实例变量,并为其分配后缀字符串文字,并使用@IdSuffix进行注释。 (对于前缀,@ IdPrefix)。此字段不会保留在couchbase中,仅用于生成文档的ID。

@Document
public final class Employee {

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

    @IdAttribute
    private String employeeId;

    @IdSuffix
    private String suffix = "EMPLOYEETYPE";
}

参考文献:https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration.configuration

© www.soinside.com 2019 - 2024. All rights reserved.