首页 > 解决方案 > 如何根据项目的其他属性值在 DynamoDBMapper 中自动生成属性

问题描述

假设一个学生表具有以下属性 id、course_code、joining_year、admission_number。学生 ID 是根据此模式的其他属性 {course_code}_{joining_year}_{admission_number} 自动生成的。这可以使用 AWS DynamoDBMapper @DynamoDBAutoGenerated 来完成吗?如果有怎么办?还是有其他方法可以做到这一点?

标签: amazon-dynamodb

解决方案


它无法完成,因为自动生成器 api 不允许您自省 POJO 中的任何其他字段。但是,您可以通过getStudentId()使用 POJO 中的其他字段计算值来获得所需的结果。

例如:

String getStudentId() {
    if (this.studentId == null) {
        this.studentId = String.format("%s_%s_%s", this.courseCode, this.joiningYear, this.admissionNumber);
    }
    return this.studentId;
}

推荐阅读