[英]Spring Data DynamoDB - Respository performing scan instead of query
Repository perform a query over a method that only uses hashKey and rangeKey attributes, and result this:存储库对仅使用 hashKey 和 rangeKey 属性的方法执行查询,结果如下:
"{"TableName":"music","KeyConditionExpression":"artist = :_artist and begins_with(title, :_title)","ExpressionAttributeValues":{":_artist":{"S":"Djavan"},":_title":{"S":"Eu te devoro"}}}"
Repository perform a scanFilter over a method that only uses hashKey and rangeKey attributes, and result this:存储库对仅使用 hashKey 和 rangeKey 属性的方法执行 scanFilter ,结果如下:
"{"TableName":"music","ScanFilter":{"artist":{"AttributeValueList":[{"S":"Djavan"}],"ComparisonOperator":"EQ"},"title":{"AttributeValueList":[{"S":"Eu te devoro"}],"ComparisonOperator":"BEGINS_WITH"}}}"
Using a entity named Music使用名为 Music 的实体
@DynamoDBTable(tableName = "music")
data class Music(
@field:Id
@DynamoDBIgnore
val id: MusicId = MusicId(),
var genre: String? = null
) {
@DynamoDBHashKey(attributeName = "artist")
fun getArtist() = id.artist
fun setArtist(artist: String) {
id.artist = artist
}
@DynamoDBHashKey(attributeName = "title")
fun getTitle() = id.title
fun setTitle(title: String) {
id.title = title
}
}
@DynamoDBDocument
data class MusicId(
@field:DynamoDBHashKey(attributeName = "artist")
var artist: String? = null,
@field:DynamoDBRangeKey(attributeName = "title")
var title: String? = null
) : Serializable
And a repository和一个存储库
@EnableScan //I know that if I remove this annotation, enables won't be permitted, but the problem is that the implementation code doesn't recognize my method as a key query and if I remove this annotation, the method falls on invocation
interface MusicRepository : CrudRepository<Music, MusicId> {
fun findByArtistAndTitleStartingWith(artista: String, sortKey: String): List<Music>
}
And when i invoke:当我调用时:
@PostConstruct
fun init() {
println(musicRepository.findByArtistAndTitleStartingWith("Djavan", "Eu te devoro").joinToString())
}
the log show's me the call to AWS as i showed above日志显示了我上面显示的对 AWS 的调用
did I something wrong?我做错了什么吗? Or is the other approach that spring data create the correct query to aws?
还是 spring 数据为 aws 创建正确查询的另一种方法?
After trying random changes in entity.在尝试随机更改实体后。 I got the expected result in the repository method.
我在存储库方法中得到了预期的结果。
Correct way to map a Entity with composite key使用复合键映射实体的正确方法
@DynamoDBTable(tableName = "music")
data class Music(
@get:DynamoDBHashKey(attributeName = "artist")
var artist: String? = null,
@get:DynamoDBRangeKey(attributeName = "title")
var title: String? = null,
var genre: String? = null
) {
@Id
private var id: MusicId? = null
get() = MusicId(artist, title)
}
@DynamoDBDocument
data class MusicId(
@field:DynamoDBHashKey(attributeName = "artist")
var artist: String? = null,
@field:DynamoDBRangeKey(attributeName = "title")
var title: String? = null
) : Serializable
After change the entity and invoke:更改实体并调用后:
musicRepository.findByArtistAndTitleStartingWith("Djavan", "Eu te devoro")
I got the corret invocation of AWS api.我得到了 AWS api 的正确调用。
"{"TableName":"music","ConsistentRead":true,"KeyConditions":{"artist":{"AttributeValueList":[{"S":"Djavan"}],"ComparisonOperator":"EQ"},"title":{"AttributeValueList":[{"S":"Eu te devoro"}],"ComparisonOperator":"BEGINS_WITH"}},"ScanIndexForward":true}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.