0
votes

Expected Behavior

Repository perform a query over a method that only uses hashKey and rangeKey attributes, and result this:

"{"TableName":"music","KeyConditionExpression":"artist = :_artist and begins_with(title, :_title)","ExpressionAttributeValues":{":_artist":{"S":"Djavan"},":_title":{"S":"Eu te devoro"}}}"

Actual Behavior

Repository perform a scanFilter over a method that only uses hashKey and rangeKey attributes, and result this:

"{"TableName":"music","ScanFilter":{"artist":{"AttributeValueList":[{"S":"Djavan"}],"ComparisonOperator":"EQ"},"title":{"AttributeValueList":[{"S":"Eu te devoro"}],"ComparisonOperator":"BEGINS_WITH"}}}"

Steps to Reproduce the Problem

Using a entity named 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

Specifications

Github issue

did I something wrong? Or is the other approach that spring data create the correct query to aws?

1

1 Answers

1
votes

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.

"{"TableName":"music","ConsistentRead":true,"KeyConditions":{"artist":{"AttributeValueList":[{"S":"Djavan"}],"ComparisonOperator":"EQ"},"title":{"AttributeValueList":[{"S":"Eu te devoro"}],"ComparisonOperator":"BEGINS_WITH"}},"ScanIndexForward":true}"