Skip to content

@DynamoDbImmutable is ignored. Error: "DynamoDb bean class must be annotated with @DynamoDbBean" #2216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bigunyak opened this issue Dec 21, 2020 · 5 comments · Fixed by #2227
Labels
documentation This is a problem with documentation.

Comments

@bigunyak
Copy link

Can't get example from the docs with @DynamoDbImmutable working.

Describe the bug

Attempts to create a DynamoDbTable object fail with "A DynamoDb bean class must be annotated with @DynamoDbBean" error.

Expected Behavior

Should be no error.

Current Behavior

Error stack trace is below.

Caused by: java.lang.IllegalArgumentException: A DynamoDb bean class must be annotated with @DynamoDbBean
	at software.amazon.awssdk.enhanced.dynamodb.mapper.BeanTableSchema.createStaticTableSchema(BeanTableSchema.java:156) ~[dynamodb-enhanced-2.15.49.jar!/:?]
	at software.amazon.awssdk.enhanced.dynamodb.mapper.BeanTableSchema.create(BeanTableSchema.java:124) ~[dynamodb-enhanced-2.15.49.jar!/:?]
	at software.amazon.awssdk.enhanced.dynamodb.mapper.BeanTableSchema.create(BeanTableSchema.java:116) ~[dynamodb-enhanced-2.15.49.jar!/:?]
	at software.amazon.awssdk.enhanced.dynamodb.TableSchema.fromBean(TableSchema.java:80) ~[dynamodb-enhanced-2.15.49.jar!/:?]
	at no.example.customer_store.CustomerStoreDAO.<init>(CustomerStoreDAO.java:23) ~[classes!/:1]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_242]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_242]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_242]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_242]
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:203) ~[spring-beans-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310) ~[spring-beans-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]
	... 27 more

Steps to Reproduce

The following code works fine.

@DynamoDbBean
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
    String primaryKey;
    String sortKey;
    Instant created;

    @DynamoDbPartitionKey
    @DynamoDbAttribute("PK")
    public String getPrimaryKey() {
        return this.primaryKey;
    }

    @DynamoDbSortKey
    @DynamoDbAttribute("SK")
    public String getSortKey() {
        return this.sortKey;
    }
}

This code fails.

@Value
@Builder
@DynamoDbImmutable(builder = Customer.CustomerBuilder.class)
public class Customer {
    String primaryKey;
    String sortKey;
    Instant created;

    @DynamoDbPartitionKey
    @DynamoDbAttribute("PK")
    public String getPrimaryKey() {
        return this.primaryKey;
    }

    @DynamoDbSortKey
    @DynamoDbAttribute("SK")
    public String getSortKey() {
        return this.sortKey;
    }
}

Code from the example without Lombok also fails.

@DynamoDbImmutable(builder = Customer.Builder.class)
public class Customer {
    private final String primaryKey;
    private final String sortKey;
    private final Instant created;

    private Customer(Builder b) {
        this.primaryKey = b.primaryKey;
        this.sortKey = b.sortKey;
        this.created = b.created;
    }

    public static Builder builder() {
        return new Builder();
    }

    @DynamoDbPartitionKey
    @DynamoDbAttribute("PK")
    public String getPrimaryKey() {
        return this.primaryKey;
    }

    @DynamoDbSortKey
    @DynamoDbAttribute("SK")
    public String getSortKey() {
        return this.sortKey;
    }

    public static final class Builder {
        private String primaryKey;
        private String sortKey;
        private Instant created;

        public Builder primaryKey(String primaryKey) {
            this.primaryKey = primaryKey;
            return this;
        }

        public Builder sortKey(String sortKey) {
            this.sortKey = sortKey;
            return this;
        }

        public Builder created(Instant created) {
            this.created = created;
            return this;
        }

        public Customer build() {
            return new Customer(this);
        }
    }
}

DynamoDbTable object is created like this.

public class CustomerStoreDAO {
    private DynamoDbTable<Customer> customerStoreTable;

    public CustomerStoreDAO(
            CustomerContext customerContext,
            DynamoDbEnhancedClient dynamoDbEnhancedClient) {
        this.customerStoreTable = dynamoDbEnhancedClient.table(
                "customer", TableSchema.fromBean(Customer.class));
    }
}

Possible Solution

Context

Your Environment

  • AWS Java SDK version used: 2.15.49
  • JDK version used: AdoptOpenJDK (build 1.8.0_242-b08
  • Operating System and version: macOS Catalina 10.15.7
@bigunyak bigunyak added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 21, 2020
@mahtabsabet
Copy link

I was having this same issue - the problem was I was calling .fromBean(). When I changed it to .fromImmutableClass(), it started working.

@debora-ito
Copy link
Member

@bigunyak does calling .fromImmutableClass() work in your case?

Can you post a link to the example?

@debora-ito debora-ito added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. and removed needs-triage This issue or PR still needs to be triaged. labels Dec 23, 2020
@bigunyak
Copy link
Author

@mahtabsabet yes, that works! Thanks a lot for sharing your finding! :)

@debora-ito, not sure which example you mean? I posted the code I tried.
It does work when using .fromImmutableClass() instead of .fromBean(), but this really must be mentioned in the documentation here: https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. label Dec 23, 2020
@debora-ito
Copy link
Member

@bigunyak that's the documentation link I was hoping to get, I'll add this info to make it more clear.

@debora-ito debora-ito added documentation This is a problem with documentation. and removed bug This issue is a bug. labels Jan 4, 2021
@github-actions
Copy link

github-actions bot commented Jan 6, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

aws-sdk-java-automation added a commit that referenced this issue Oct 20, 2022
…a22734050

Pull request: release <- staging/28bb2ccf-ad27-4134-8a03-d56a22734050
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants