类型java.lang.String中没有@DynamoDBHashKey注释的方法或字段

时间:2019-04-18 03:41:52

标签: java amazon-web-services nosql spring-data amazon-dynamodb

我不知道为什么不运行这个项目。

@Data
@DynamoDBTable(tableName = "tableName")
public class entityName implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @DynamoDBHashKey
    @DynamoDBAutoGeneratedKey
    private String id;

    @DynamoDBRangeKey
    private String rk;

    private String sy;
}

Caused by: java.lang.IllegalArgumentException: No method or field annotated by @DynamoDBHashKey within type java.lang.String!
    at org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBHashAndRangeKeyMethodExtractorImpl.<init>(DynamoDBHashAndRangeKeyMethodExtractorImpl.java:99) ~[spring-data-dynamodb-5.1.0.jar:5.1.0]
    at org.socialsignin.spring.data.dynamodb.repository.support.CompositeIdHashAndRangeKeyExtractor.<init>(CompositeIdHashAndRangeKeyExtractor.java:31) ~[spring-data-dynamodb-5.1.0.jar:5.1.0]
    at org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.getHashAndRangeKeyExtractor(DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java:71) ~[spring-data-dynamodb-5.1.0.jar:5.1.0]
    at org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBIdIsHashAndRangeKeyEntityInformationImpl.<init>(DynamoDBIdIsHashAndRangeKeyEntityInformationImpl.java:49) ~[spring-data-dynamodb-5.1.0.jar:5.1.0]
    at org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityMetadataSupport.getEntityInformation(DynamoDBEntityMetadataSupport.java:125) ~[spring-data-dynamodb-5.1.0.jar:5.1.0]
    at org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactory.getEntityInformation(DynamoDBRepositoryFactory.java:104) ~[spring-data-dynamodb-5.1.0.jar:5.1.0]
    at org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactory.getDynamoDBRepository(DynamoDBRepositoryFactory.java:128) ~[spring-data-dynamodb-5.1.0.jar:5.1.0]
    at org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactory.getTargetRepository(DynamoDBRepositoryFactory.java:150) ~[spring-data-dynamodb-5.1.0.jar:5.1.0]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:305) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:211) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    ... 47 common frames omitted

2 个答案:

答案 0 :(得分:0)

您必须具有该字段的getter和setter,如文档中所述:

  

如果注释直接应用于class字段,则必须在同一类中声明相应的getter和setter。   https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBHashKey.html

另请参阅开发者指南:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.Annotations.html#DynamoDBMapper.Annotations.DynamoDBHashKey

答案 1 :(得分:0)

我认为问题是这样的:

@DynamoDBRangeKey
private String rk;

我注意到您有一个@Id批注,这意味着您可能有一个看起来像这样的Spring Data Repository:

@Repository
@EnableScan
public interface ServiceItemRepository extends CrudRepository<ServiceItem, String> {
}

@Id批注向Spring Data发出信号,表明这是Repository声明中String泛型的主题。但是复合键似乎使事情变得混乱。基本上,如果您删除@Id而不在此处使用存储库,或者将@DynamoDBRangeKey替换为普通的@DynamoDBAttribute。您会发现事情再次开始起作用。

顺便说一句:龙目岛与问题无关。