Database columns that allow null values can be mapped to Option[X]
. The field value will be set to Some(X)
or None
.
case class Dog(val name: Option[String])
object DogEntity extends Entity[Int,SurrogateIntId, Dog] {
val id = key("id") autogenerated (_.id)
val name = column("name") option (_.name)
def constructor(implicit m) = new Dog(name) with Stored {
val id: Int = DogEntity.id
}
}
Similarily, relationships that use Option[X]
can be mapped:
case class Category(val name: String, val parent: Option[Category], val linked: Option[Category])
object CategoryEntity extends Entity[Int,SurrogateIntId, Category] {
val id = key("id") autogenerated (_.id)
val name = column("name") to (_.name)
// self reference
val parent = manytoone(CategoryEntity) foreignkey "parent_id" option (_.parent)
val linked = onetoone(CategoryEntity) option (_.linked)
def constructor(implicit m) =
new Category(name, parent, linked) with Stored {
val id: Int = CategoryEntity.id
}
}