2

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
2
  • try to ask a question in details. Commented Apr 18, 2019 at 4:14
  • Code formating. Commented Apr 18, 2019 at 7:48

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

As pointed out by @michael-coxon the issue is with composite keys. Therefore, you would need to create a composite key class as described here.

https://github.com/derjust/spring-data-dynamodb/wiki/Use-Hash-Range-keys

Comments

0

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

4 Comments

Did you notice the Project Lombok annotation @Data? I think the OP would have thought that the getters and setters would have been supplied by the generated accessors. Its interesting that this is not being recognised.
Lombok should have generated the accessors before runtime, so effectively, the corresponding getter and setter are being declared in the same class.
Then one has to check if these getters (and setters) exist before the AWS annotations are examined.
I was meaning to answer this, which I have just done - it turns out that the getters and setters weren't the problem, but its still a bit weird that it doesn't work. I mean I know the basic cause, but not the root cause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.