DynamoDBMapper can store List<CustomObject> but not Set<CustomObject>
Why is it that DynamoDBMapper can marshal a List
Please see the below example which works perfectly until you replace List
com.amazonaws.services.dynamodbv2.datamodeling.marshallers.ObjectSetToStringSetMarshaller: Marshaling a set of non-String objects to a DynamoDB StringSet. You won't be able to read these objects back out of DynamoDB unless you REALLY know what you're doing: it's probably a bug. If you DO know what you're doing feelfree to ignore this warning, but consider using a custom marshaler for this instead.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/application-context.xml")
public class ProductIntegrationTest {
@Autowired
AmazonDynamoDBClient dynamoDBClient;
@Test
public void shouldStoreNewProduct() {
DynamoDBMapper productMapper = new DynamoDBMapper(dynamoDBClient);
User user1 = new User();
user1.setUserId(1);
user1.setFirstName("user1");
User user2 = new User();
user2.setUserId(2);
user2.setFirstName("user2");
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
Product p = new Product();
p.setProductName("productA");
p.setUsers(users);
productMapper.save(p);
}
}
Product
@DynamoDBTable(tableName = "products")
public class Product {
private String productName;
private List<User> users;
public Product() {
}
@DynamoDBHashKey(attributeName = "productName")
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@DynamoDBAttribute(attributeName = "users")
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
User
@DynamoDBDocument
public class User {
private String firstName;
private long userId;
public User(){
}
@DynamoDBAttribute(attributeName = "firstName")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@DynamoDBAttribute(attributeName = "userId")
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
}
I can confirm the issue.
Neither
new DynamoDBMapper(dynamoDB,DynamoDBMapperConfig.builder().withConversionSchema(ConversionSchemas.V2).build());
nor
new DynamoDBMapper(dynamoDB, new DynamoDBMapperConfig(ConversionSchemas.V2));
works as expected. DynamoDBMapper still invokes toString() for objects in Set making it become a StringSet.