java - Couchbase - Auto generate key with user defined suffix
问题描述
I want to save an employee object in couchbase using spring boot java. I am using reactive couchbase driver. My requirement is to save the employee object with employeeId suffixed with hard coded string "-EMPLOYEETYPE".
Example: Object to couchbase from Java Application:
{ "employeeId" : "12345", "lname" :"ltest", "fname" : "ftest"}
While saving to couch base, key supposed to be generated like
"12345-EMPLOYEETYPE"
Below code is not working, kindly guide me how to achieve it. Note: I am using lombok so there are no getters and setters.
@Document
public final class Employee {
@Id @GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES,delimiter="-EMPLOYEETYPE")
private String id;
@IdAttribute
private String employeeId;
}
解决方案
找到了解决方案。我们需要创建一个实例变量,并为其分配后缀字符串文字,并使用@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";
}
推荐阅读
- r - cmd 中的 rmarkdown::render() 返回 pandoc 错误
- oracle-apex - 通过api创建新记录时,在apex oracle中自动打开模式窗口
- graphql - 如何使用 SitePage 查询填充搜索索引?
- python - 我们可以用 MIPCL “热启动” MILP 优化和初始可行解决方案吗?
- video.js - Videojs PIP 选项在 IE 和 Edge 中不起作用
- java - 读取 BinaryWebSocketFrame 数据 PooledUnsafeDirectByteBuf
- tomcat9 - 在 ubuntu 20.04 上未正确安装 tomcat9
- regex - camelCase 到snake_case 使用shell?
- javascript - 我可以解决html视频标签的问题吗
- java - 将 JSON 解析为嵌套类