Aws-sdk-java-v2: @DynamoDbImmutable is ignored. Error: "DynamoDb bean class must be annotated with @DynamoDbBean"

Created on 21 Dec 2020  路  5Comments  路  Source: aws/aws-sdk-java-v2

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
documentation

Most helpful comment

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

All 5 comments

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

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

Can you post a link to the example?

@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

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

鈿狅笍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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tigertoes picture tigertoes  路  6Comments

shorea picture shorea  路  4Comments

millems picture millems  路  6Comments

deepankerk picture deepankerk  路  5Comments

ceven picture ceven  路  5Comments