I have nested classes/objects and want to store (and retrieve) them in a database by using SLICK. I understand that with SLICK mapped projection would be key. Furthermore I use a companion object to map between nested objects and flat structure (to be stored in the DB table). I want to do something like this (simplified example):
case class Foo(id: Int, myBar: Bar)
case class Bar(myInt: Int, myString: String)
object Foo {
def apply(id: Int, myInt: Int, myString: String): Foo = Foo(id, Bar(myInt, myString))
override def unapply(f: Foo) = (f.id, f.myBar.myInt, f.myBar.myString)
}
object TTable extends Table[Foo]("FOO") {
def id = column[Int]("id", O.PrimaryKey)
def myInt = column[Int]("myInt", O NotNull)
def myString = column[String]("myString", O NotNull)
def * = id ~ myInt ~ myString <> (Foo.apply _, Foo.unapply _)
def query(db: Database, id: Int): Option[Foo] = db withSession { //s: Session =>
(for { b <- TTable if b.id is id} yield b).firstOption
}
}
But the compilation fails with several errors: "method unapply is defined twice", "ambiguous reference to overloaded definition, both method apply [...] match expected type ?" and "overloaded method value <> with alternatives"
I found this excellent explanation of mapped projection "scala slick method I can not understand so far" and "Mapped projection with <> to a case class with companion object in Slick" but none of the suggested solutions work for me.