Can't get example from the docs with @DynamoDbImmutable working.
Attempts to create a DynamoDbTable object fail with "A DynamoDb bean class must be annotated with @DynamoDbBean" error.
Should be no error.
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
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));
}
}
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.
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.
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.