No method or field annotated by @DynamoDBHashKey w

2019-08-16 06:39发布

问题:

I don't know why doesn't run this project.

@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

回答1:

You have to have getters and setters for the field, as in the documentation is stated:

If the annotation is applied directly to the class field, the corresponding getter and setter must be declared in the same class. https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBHashKey.html

Also have a look at the developer guide: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.Annotations.html#DynamoDBMapper.Annotations.DynamoDBHashKey



回答2:

I think the problem is this bit:

@DynamoDBRangeKey
private String rk;

I noticed that you have an @Id annotation, which means you probably have a Spring Data Repository looking something like this:

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

The @Id annotation is a signal to Spring Data that this is the subject of the String generic in the Repository declaration. But composite keys seem to confuse the hell out of things. Basically, if you either remove the @Id and don't use a Repository here, or replace the @DynamoDBRangeKey with an ordinary @DynamoDBAttribute. You'll find things starting to work again.

BTW: Lombok had nothing to do with the problem.