Slick Mapping sub entities to case classes where the columns are in the same table.

288 views
Skip to first unread message

Leon Radley

unread,
Mar 12, 2013, 4:02:47 AM3/12/13
to scala...@googlegroups.com
Hi!

I'm trying to implement securesocial (authentication plugin for Play2) using slick.

The problem I'm having is that I wan't to store everything in the same table, but there are a couple of entities that are case classes that should be sub entities of the User.

My question is how should I do the mapping of the columns so that they correctly get converted into their respective case classes?

But it felt like a work around.

Any suggestions would be appreciated!


Leon Radley

unread,
Mar 13, 2013, 4:51:17 AM3/13/13
to scala...@googlegroups.com
Asked the question on stackoverflow, to get some more help

Clint Gilbert

unread,
Mar 13, 2013, 4:47:17 PM3/13/13
to scala...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 03/12/2013 04:02 AM, Leon Radley wrote:
> *My question is* how should I do the mapping of the columns so that
> they correctly get converted into their respective case classes?

I could easily be misunderstanding, but it strikes me that you could
use functions other than Foo.apply and Foo.unapply when using the <>
operator.

Say you had

case class Bar(x: Int, y: Int)

case class Baz(a: Int, b: Int, c: Int)

case class Foo(bar: Bar, baz: Baz)

and a table where a Foo is stored flattened into a row with 5 columns,
you could define your own functions that pack and unpack a Foo, like:

object MyTable extends Table[Foo]("my_table") {
def x = column[Int]("x")
def y = column[Int]("y")
def a = column[Int]("a")
def b = column[Int]("b")
def c = column[Int]("c")

def makeFoo(x: Int, y: Int, a: Int, b: Int, c: Int): Foo = {
Foo(Bar(x, y), Baz(a, b, c))
}

def unpackFoo(foo: Foo): (Int, Int, Int, Int, Int) = {
val Foo(Bar(x, y), Baz(a, b, c)) = foo

(x, y, a, b, c)
}

//not sure if _ is needed here, but you get the gist
override def * = x ~ y ~ a ~ b ~ c <> (makeFoo _, unpackFoo _)
}


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iEYEARECAAYFAlFA5dUACgkQ5IyIbnMUeTunVwCdE9wDYzM7oYrXAuOedJcXPodU
yogAn1t0Qb1RMv7J6EklW2i7/v7Q4L+F
=IzAJ
-----END PGP SIGNATURE-----
Reply all
Reply to author
Forward
0 new messages