How to persist a map?

41 views
Skip to first unread message

nepomu...@gmail.com

unread,
Mar 12, 2015, 4:38:37 AM3/12/15
to activate-p...@googlegroups.com
Hi,

I'm wondering what to do to have a map field in an entity, like

class MyEntity(val map: Map[String, Int]) extends Entity

As maps are not supported natively, I thought about using en-/decoding to List[(String, Int)]. Is this the preferred way?

Thanks,
Nepomuk

Flavio W. Brasil

unread,
Mar 12, 2015, 9:19:58 AM3/12/15
to nepomu...@gmail.com, activate-p...@googlegroups.com
Hi,

How do you plan to encode/decode the List[(String, Int)]? Using an opaque value at the database level?

Best,

-- 
Flavio W. Brasil

--
You received this message because you are subscribed to the Google Groups "Activate Persistence Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to activate-persist...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sebasti...@googlemail.com

unread,
Mar 15, 2015, 5:32:50 AM3/15/15
to activate-p...@googlegroups.com, nepomu...@gmail.com
Hi Flavio,

good question: I thought as Activate supports immutable.List natively, it would work automatically. Unfortunately, it didn't. Is there any example how to persist maps?

Regards,
Nepomuk

Flavio W. Brasil

unread,
Mar 15, 2015, 5:40:28 AM3/15/15
to sebasti...@googlemail.com, activate-p...@googlegroups.com, nepomu...@gmail.com
Hi Nepomuk,

It depends on how you want to have the values persisted at the database level. I normally use a list of entities, so it is possible to persist it without using opaque column values:

case class MyEntityMapValue(key: String, value: String) extend Entity
class MyEntity(private val _map: List[MyEntityMapValue]) extend Entity {
def map = _map.map(kv => (kv.key, kv.value)).toMap
}

Ideally, Activate should have native support for maps.

-- 
Flavio W. Brasil

--
Reply all
Reply to author
Forward
0 new messages