How to match object's id:Pk[T] in Anorm?
-
29-06-2021 - |
Question
How to check if the object was already been saved to database ( e.g. by matching its id)?
Something similar to the following snippet:
case class User(id: Pk[Long], name:String) {
def loaded = id match {
case NotAssigned => false
case Pk(refererId) => true
}
}
Solution
In this example, you want:
import anorm.Id // if you don't already have import anorm._
case class User(id: Pk[Long], name:String) {
def loaded = id match {
case NotAssigned => false
case Id(refererId) => true
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow