aws-sdk-java: DynamoDBMapper can store List but not Set
Why is it that DynamoDBMapper can marshal a List<User> but not a Set<User> when a Set<User> could easily be stored in Dynamo without loss of precision using a List?
Please see the below example which works perfectly until you replace List<User> with Set<User>, however when you do this you get the following exception.
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;
}
}
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Comments: 21 (7 by maintainers)
As we discussed above, we kept the original (admittedly pretty strange) behavior of the mapper by default to avoid breaking backwards-compatibility. To get the new behavior, you need to explicitly configure the mapper to use the new V2 conversion schema.
We should get that WARN message updated to mention the V2 schema (and document it better in general).
Hey Cathal,
Once upon a time, in the dark ages before DynamoDB supported List shapes, the mapper only supported Sets of a few types that we could reliably round-trip through DynamoDB’s StringSet, NumberSet, and BinarySet types. Due to a quirk in the original implementation however, the mapper would actually successfully save Sets of arbitrary types into DynamoDB; they took the Set<String> codepath, were converted into Strings via toString(), and these strings were stored in a DynamoDB StringSet.
When we retooled the mapper to add support for Lists/Maps, we decided it was important to keep this behavior backwards-compatible by default in case anyone was depending on it. Hence ObjectSetToStringSetMarshaller, which explicitly performs this same conversion but logs a warning if it’s ever used.
If you explicitly opt in to the V2 conversion schema, we no longer register the ObjectSetToStringSetMarshaller, so we could definitely look at adding support for a new ObjectSetToListMarshaller and corresponding ListToObjectSetUnmarshaller there. Does that sound reasonable to you?
@david-at-aws . Hi, I have found the recommended style to do this. That is
DynamoDBMapperConfig config = DynamoDBMapperConfig.builder().withConversionSchema(ConversionSchemas.V2).build();.Is it right ?
@david-at-aws It would be great if the DDB documentation could be updated with this. The java sdk supported data types in the docs don’t have any information about Map and List support. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.DataTypes.html