【问题标题】:AWS DynamoDBMapper save method keeps throwing `DynamoDBMappingException: not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted`AWS DynamoDBMapper 保存方法不断抛出 `DynamoDBMappingException: not supported;需要@DynamoDBTyped 或@DynamoDBTypeConverted`
【发布时间】:2019-03-13 20:25:16
【问题描述】:

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

【问题讨论】:

  • 非常透明地描述了您的路径。但是可以将抛出的指示异常添加为堆栈跟踪。

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


【解决方案1】:

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

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

@DynamoDBIgnore
private RecentlyPlayedId recentlyPlayedId;

您还需要删除@id 注释。

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

【讨论】:

  • 添加@DynamoDBIgnore 注释而不是@Id 抱怨没有id 注释。将它们放在一起会引发相同的错误。
  • 好的,我看到您使用的是第三方库而不是 DynamoDBMapper。祝你好运!
  • 嗯。我正在使用com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper
  • @id 不是 dynamodbmapper 注释
  • 同时使用 Spring 和 DynamoDBMapper 似乎是个坏主意。
【解决方案2】:

删除 @Id 字段的 getter/setter 为我解决了这个问题。这是在https://github.com/derjust/spring-data-dynamodb/wiki/Use-Hash-Range-keys 中建议的

【讨论】:

    【解决方案3】:

    不支持;需要@DynamoDBTyped 或@DynamoDBTypeConverted",

    当我使用字段 JsonNode 定义模型类时遇到此错误,我将其转换为 MAP,现在它工作正常

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2018-05-07
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2020-02-16
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多