我在Scala代码中使用Amazon的DynamoDBMapper时遇到了麻烦。主要的关键点是让JVM在案例类中使用时识别@DynamoDBHashkey,如:
case class MyCoolCaseClass(@DynamoDBHashKey(attributeName = "my_id") myId: String) {}
将此客户端库集成到Scala项目中的人的任何指示? (我希望不要简单地回到低级API,虽然这可能是一个不错的决定,一旦用Mapper耗尽我的选择)。
答案 0 :(得分:10)
DynamoDB映射器使用反射来查找getter和setter。 SDK采用Java风格的约定,即getter和setter以“get”或“is”开头,setter以“set”开头。您可以看到反射代码on github。
我已经能够使它工作,但感觉就像编写Java :(
@DynamoDBTable(tableName = "awesome_table")
class TheBestClass {
private var id : Integer = _
@DynamoDBHashKey
def getId() = id
def setId(_id: Integer) = id = _id
}
答案 1 :(得分:10)
我必须这样做:
import annotation.meta.beanGetter
import beans.BeanProperty
import com.amazonaws.services.dynamodbv2.datamodeling._
@DynamoDBTable(tableName="DEMOTAB")
case class DemoItem( // it's a case class for the free stuff, but can be ordinary class
@(DynamoDBHashKey @beanGetter) // would not work without meta annotation
@BeanProperty var id:String, // must be var or mapper can't instantiate one
@BeanProperty var number:Integer
) {
def this() = this(null, null) // needed by DynamoDB Mapper to instantiate
}
答案 2 :(得分:0)
这对我有用,包括boolean
@DynamoDBTable(tableName = "User")
case class User(
@(DynamoDBHashKey @field)
@(DynamoDBAutoGeneratedKey @field)
@BeanProperty var id: String,
@(DynamoDBAttribute @field)
@BeanProperty var firstName: String,
@(DynamoDBAttribute @field)
@BeanProperty var lastName: String,
@(DynamoDBAttribute @field)
@BeanProperty var active: Boolean
)
{
def this() = this(null, null, null, false)
}