derjust / spring-data-dynamodb

This module deals with enhanced support for a data access layer built on AWS DynamoDB.
https://derjust.github.io/spring-data-dynamodb/
Apache License 2.0
403 stars 141 forks source link

Table with hash and range key in Kotlin #240

Closed rheber closed 5 years ago

rheber commented 5 years ago

Expected Behavior

I'm working on a Kotlin program and a table I'm writing a crud repository for uses both a hash and range key. After consulting this page:

https://github.com/derjust/spring-data-dynamodb/wiki/Use-Hash-Range-keys

I've written what I believe to be the Kotlin equivalent of the necessary code. As such I would expect the code to compile with no errors.

Actual Behavior

Attempting to save an entry to the table produces this exception:

com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: One of the required keys was not given a value

Steps to Reproduce the Problem

Here are the relevant sections of the code:

interface FoobarRepository: CrudRepository<FoobarEntry, FoobarEntryId> {
    fun findAllByFoobarCode(foobarCode: String): Iterable<FoobarEntry>
}

data class FoobarEntryId (
    @get:DynamoDBHashKey var foobarCode: String? = null,
    @get:DynamoDBRangeKey var foobarKey: String? = null
)

@DynamoDBTable(tableName = "Foobars")
class FoobarEntry(
    @Id
    private var foobarEntryId: FoobarEntryId? = null,

    @get:DynamoDBHashKey
    @get:DynamoDBAttribute
    var foobarCode: String? = null,

    @get:DynamoDBRangeKey
    @get:DynamoDBAttribute
    var foobarKey: String? = null,

    @get:DynamoDBAttribute
    var foobarValue: String? = null
)

@Component
class LocalDatabase(
    val foobarRepository: FoobarRepository
) {

    @PostConstruct
    fun start () {
        setupTestData()
    }

    private fun setupTestData() {
            foobarRepository.save(FoobarEntry(
            foobarEntryId = FoobarEntryId(foobarCode = "1", foobarKey = "key"),
            foobarCode = "1",
            foobarKey = "key",
            foobarValue = "value"
        ))
    }
}

The exception occurs when trying to instantiate a LocalDatabase bean.

Specifications

alex-arana commented 5 years ago

I've created an article in the project wiki to demonstrate the modelling of Hash/Range partition keys using Spring-Data-style composite primary keys in Kotlin:

Composite Primary Keys Kotlin Example

Feel free to close the ticket if this solves your issue.

rheber commented 5 years ago

Thanks @alex-arana. It turned out I just had a typo.