问题描述
我正在尝试为以下情况创建dynamoDBMapper注释.
I am trying to create a dynamoDBMapper annotation for the below case.
我有EmployeeLevelTrail,它是Employee级记录的一个类
I have EmployeeLevelTrail which is a class of a Employee level record
@DynamoDBTable(tableName = TABLE_NAME)
public class EmployeeData {
public final static String TABLE_NAME = "EmployeeDataRecord";
@DynamoDBAttribute(attributeName = "employeeID")
public String EmployeeID;
@DynamoDBAttribute(attributeName = "EmployeeLevelDataRecords")
@DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.M)
public EmployeeLevelTrail employeeLevelTrail
}
public class EmployeeLevelTrail {
public final static String DDB_ATTR_EMPLOYEE_LEVEL_TRAIL = "employeeLevelTrail";
@DynamoDBAttribute(attributeName = DDB_ATTR_EMPLOYEE_LEVEL_TRAIL)
private List<EmployeeLevelRecord> thisEmployeeLevelRecords;
public void appendEmployeeLevelRecord(@NonNull EmployeeLevelRecord employeeLevelRecord) {
thisEmployeeLevelRecords.add(employeeLevelRecord);
}
}
public class EmployeeLevelRecord {
private String Level;
private String Manager;
private Instant timeOfEvent;
}
这是我的注释,但不正确,因为我无法保存DynamoDB数据
This is my annotation but it is not correct as I am not able to save my DynamoDB data
推荐答案
我使用以下代码访问了解决方案.仅使用@DynamoDBTypeConvertedJSON,我就可以在表中创建一个条目,但由于遇到映射Dynamo DB映射异常而无法更新它.
I reached to the solution using the below code. With only @DynamoDBTypeConvertedJSON, I was able to create an entry in the table but not update it as I was running into mapping Dynamo DB mapping exception.
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@DynamoDBTable(tableName = TABLE_NAME)
public class EmployeeData {
public final static String TABLE_NAME = "EmployeeData";
public final static String DDB_ATTR_ID = "Id";
public final static String DDB_ATTR_EMPLOYEE_LEVEL_RECORD_TRAIL = "EmployeeLevelRecordTrail";
@DynamoDBHashKey(attributeName = DDB_ATTR_ID)
@DynamoDBAttribute(attributeName = DDB_ATTR_ID)
private String id;
@DynamoDBAttribute(attributeName = DDB_ATTR_EMPLOYEE_LEVEL_RECORD_TRAIL)
@DynamoDBTypeConverted(converter = EmployeeLevelRecordTrailConverter.class)
private EmployeeLevelRecordTrail employeeLevelRecordTrail;
@DynamoDBAttribute(attributeName = DDB_ATTR_CREATED_TIME)
@DynamoDBTypeConverted(converter = InstantConverter.class)
private Instant joiningTime;
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EmployeeLevelRecordTrail {
private List<EmployeeLevelRecord> thisEmployeeLevelRecords;
public void appendEmployeeLevelRecord(@NonNull EmployeeLevelRecord employeeLevelRecord) {
thisEmployeeLevelRecords.add(employeeLevelRecord);
}
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@DynamoDBDocument
public class EmployeeLevelRecord {
private String Level;
private String Manager;
@DynamoDBTypeConverted(converter = InstantConverter.class)
private Instant timeOfEvent;
}
public class EmployeeLevelRecordTrailConverter implements
DynamoDBTypeConverter<List<EmployeeLevelRecord>, EmployeeLevelRecordTrail> {
@Override
public List<EmployeeLevelRecord> convert(EmployeeLevelRecordTrail employeeLevelRecordTrail) {
return employeeLevelRecordTrail.getEmployeeLevelRecord();
}
@Override
public EmployeeLevelRecordTrail unconvert(List<EmployeeLevelRecord> thisEmployeeLevelRecords) {
return new EmployeeLevelRecordTrail(thisEmployeeLevelRecords);
}
}
public class InstantConverter implements DynamoDBTypeConverter<String, Instant> {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_INSTANT;
@Override
public String convert(Instant instant) {
return instant == null ? null : DATE_TIME_FORMATTER.format(instant);
}
@Override
public Instant unconvert(String str) {
return str == null ? null : Instant.from(DATE_TIME_FORMATTER.parse(str));
}
}
阅读有关如何进行映射的文档会有所帮助.
Reading the documentation on how to do mapping helped.
这篇关于用于对象的DynamoDB映射器注释,其中具有另一个对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!