我遵循了此处概述的所有内容 - https://github.com/derjust/spring-data-dynamodb/wiki/Use-Hash-Range-keys 。但还是没有运气。
我有一个带有哈希键和排序键的 DynamoDB 表。
这是我的实体类RecentlyPlayed.class
@DynamoDBTable(tableName="some-table")
public class RecentlyPlayed {
@Id
private RecentlyPlayedId recentlyPlayedId;
// ----- Constructor methods -----
@DynamoDBHashKey(attributeName="keyA")
// Getter and setter
@DynamoDBRangeKey(attributeName="keyB")
// Getter and setter
}
这是我的关键类RecentlyPlayedId.class
public class RecentlyPlayedId implements Serializable {
private static final long serialVersionUID = 1L;
private String keyA;
private String keyB;
public RecentlyPlayedId(String keyA, String keyB) {
this.keyA = keyA;
this.keyB = keyB;
}
@DynamoDBHashKey
// Getter and setter
@DynamoDBRangeKey
// Getter and setter
}
这是我的存储库界面RecentlyPlayedRepository
@EnableScan
public interface RecentlyPlayedRepository extends CrudRepository<RecentlyPlayed, RecentlyPlayedId> {
List<RecentlyPlayed> findAllByKeyA(@Param("keyA") String keyA);
// Finding the entry for keyA with highest keyB
RecentlyPlayed findTop1ByKeyAOrderByKeyBDesc(@Param("keyA") String keyA);
}
我正在尝试保存这样的对象
RecentlyPlayed rp = new RecentlyPlayed(...);
dynamoDBMapper.save(rp); // Throws that error
recentlyPlayedRepository.save(rp); // Also throws the same error
我正在使用Spring v2.0.1.RELEASE
。原始文档中的 wiki 警告了此错误,并描述了如何缓解该错误。我完全按照他们说的做了。但还是没有运气。
该 wiki 的链接在这里 - https://github.com/derjust/spring-data-dynamodb/wiki/Use-Hash-Range-keys
最佳答案
DynamoDB 仅支持原始数据类型,它不知道如何将复杂字段 (recentlyPlayedId) 转换为原始数据,例如字符串。
为了表明情况确实如此,您可以将注释 @DynamoDBIgnore 添加到您的recentlyPlayedId 属性中,如下所示:
@DynamoDBIgnore
private RecentlyPlayedId recentlyPlayedId;
您还需要删除@id注释。
然后您的保存功能将起作用,但最近播放的 ID 将不会存储在项目中。如果您确实想保存此字段,则需要使用 @DynamoDBTypeConverted 注解并编写转换器类。转换器类定义了如何将复杂字段转换为字符串,然后将字符串转换为复杂字段。
关于java - AWS DynamoDBMapper 保存方法不断抛出 `DynamoDBMappingException: not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55150630/