AWS DynamoDBMapper 保存方法不断抛出“DynamoDBMappingException:不支持;需要 @DynamoDBTyped 或 @DynamoDBTypeConverted`

Din*_*nan 5 java spring-data-jpa amazon-dynamodb spring-boot

我遵循了此处概述的所有内容 - 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

}
Run Code Online (Sandbox Code Playgroud)

这是我的重点课程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
}
Run Code Online (Sandbox Code Playgroud)

这是我的存储库界面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);
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试保存这样的对象

RecentlyPlayed rp = new RecentlyPlayed(...);

dynamoDBMapper.save(rp);    // Throws that error

recentlyPlayedRepository.save(rp);  // Also throws the same error
Run Code Online (Sandbox Code Playgroud)

我在用Spring v2.0.1.RELEASE。原始文档中的 wiki 警告了此错误,并描述了如何缓解该错误。我完全按照他们说的做了。但仍然没有运气。

该 wiki 的链接在这里 - https://github.com/derjust/spring-data-dynamodb/wiki/Use-Hash-Range-keys

F_S*_*O_K 6

DynamoDB 仅支持原始数据类型,它不知道如何将复杂字段(recentlyPlayedId)转换为原始数据类型,例如字符串。

为了表明情况确实如此,您可以将注释 @DynamoDBIgnore 添加到您的recentlyPlayedId 属性中,如下所示:

@DynamoDBIgnore
private RecentlyPlayedId recentlyPlayedId;
Run Code Online (Sandbox Code Playgroud)

您还需要删除@id注释。

然后您的保存功能将起作用,但最近播放的 ID 将不会存储在项目中。如果您确实想保存此字段,则需要使用 @DynamoDBTypeConverted 注解并编写转换器类。转换器类定义了如何将复杂字段转换为字符串,然后将字符串转换为复杂字段。