java - AWS DynamoDBMapper 保存方法不断抛出 `DynamoDBMappingException: not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted`

标签 java spring-boot spring-data-jpa amazon-dynamodb

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

相关文章:

java - 如何使用 Spring security 测试基于数据库的用户的 JWT 身份验证?

java - 选择具有已知自然 ID 的实体,而不是尝试插入(JPA 和 SpringBoot)

java - Spring 数据休息:How to select specific field from the repository

Java JPanel 不可见

java - 为什么我在使用 @SuppressWarnings ("unchecked"时仍然收到警告?

java - Heroku 无法检测到构建包 - Spring Boot

tomcat - 可能的 Spring Boot 或 Spring Security 内存泄漏

java - EAV 模型上的复杂选择 Spring data jpa

Java - 为什么另一个包中的子级无法通过父级引用访问父级的 protected 方法?

java - 将 xslt/Jar 转换为 EXE 文件