Persisting immutable Scala case classes with an ORM ?

38 views
Skip to first unread message

Jan Goyvaerts

unread,
Jun 3, 2011, 6:25:25 AM6/3/11
to The Java Posse, scala-user
I have a business case where the model is only composed of immutable case classes. The volume however forces us to persist the model into a database.

case class Foo (val bar: String);

Because they're immutable, is it still possible to use an ORM ? 




Michael Barker

unread,
Jun 5, 2011, 7:38:30 AM6/5/11
to java...@googlegroups.com, scala-user
Yes, but a better question might be which ORM and how?  With something like JPA you won't be able to directly map your model to the database.  However you could construct your domain objects from a set of mutable "data objects" that are mapped using JPA.  This does mean a chunk more code and more object allocation/garbage.  However it does allow your domain model and data model to evolve independently (which may or may not be a useful thing depending on your use case).  There are probably other tricks that you can do with JPA projections or different ORMs that have support for immutable objects.  You may need to shop around.

Mike.


Ferdinand de Antoni

unread,
Jun 9, 2011, 3:04:01 AM6/9/11
to java...@googlegroups.com, scala-user
If you are still free to choose your DB, or you already are using MongoDB, have a look at salat. Its a nice library to serialize/deserialize case classes.
Reply all
Reply to author
Forward
0 new messages