我正在尝试从“消息”表中获取所有记录,但问题是“角色”在 dynamo DB 中保存为数组,我需要获取每个记录具有“INSTRUCTOR”角色的所有记录。
dynamoDB 中可用的记录如下。
记录 1
{ "product": "Maths", "messageSummary": "test message", "roles": [ "ADMIN", "INSTRUCTOR" ], "title": "My course1", "createdBy": 0, "authorName" : "测试作者", "id": "1" }
记录 2
{ "product": "Maths", "messageSummary": "test message", "roles": [ "STUDENT" ], "title": "My course2", "createdBy": 0, "authorName": "test author ", "id": "2" }
记录 3
{ "product": "Maths", "messageSummary": "test message", "roles": [ "INSTRUCTOR", "STUDENT" ], "title": "My course3", "createdBy": 0, "authorName" : "测试作者", "id": "3" }
消息模型类如下,引用“消息”表
@DynamoDBTable(tableName = "Messages")
public class Message {
@Id
@ApiModelProperty(accessMode = ApiModelProperty.AccessMode.READ_ONLY, position = 1)
private String id;
/* authorName, messageSummary, title .. attributes goes here */
@ApiModelProperty(required = true, allowableValues = "maths", position = 6)
private Product product;
@ApiModelProperty(allowableValues = "student, instructor, admin", position = 7)
private List<Role> roles;
// getters and setters
}
消息存储库如下扩展 CrudRepository
@EnableScan
public interface MessageRepository extends CrudRepository<Message, String> {
List<Message> findMessageByProductAndRoles(Product product, List roles); // Need to filter by given role
}
由于我需要获取所有具有 INSTRUCTOR 角色的记录,因此记录 1 和记录 3应该在结果列表中。但是,我只能使用产品值过滤它,但不能使用角色过滤。
此外,我尝试使用一些关键字,例如包含、喜欢、在等,但这些对我来说都没有成功。根据我的观察,这些关键字不支持从数组中过滤特定值。
并得到这个错误:
{
"timestamp": "2022-01-27T08:42:54.786+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Request processing failed; nested exception is com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: One or more parameter values were invalid: ComparisonOperator CONTAINS is not valid for SS AttributeValue type (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: xxxxxxx)",
"path": "/api/xxx/my route"
}
如何使用 CrudRepository 实现这一目标并实现这样的条件?