How to use SORM framework with Play Framework?
orm, playframework, scala, sorm
Solution
- Install Play >= 2.1.0.
- Generate a project using Play's guides
Add appropriate SORM's and chosen database's dependencies to the generated `project/Build.scala`, e.g.:
val appDependencies = Seq(
"org.sorm-framework" % "sorm" % "0.3.8",
"com.h2database" % "h2" % "1.3.168"
)
In the same file make sure that your project depends on the same Scala version, on which SORM depends (for SORM 0.3.8 it's Scala 2.10.1):
val main = play.Project(appName, appVersion, appDependencies).settings(
scalaVersion := "2.10.1"
)
If you miss that step, you may bump into this issue.
In `app/models/package.scala` place all your case classes and SORM's instance declaration, e.g.:
package models
case class A( name : String )
case class B( name : String )
import sorm._
object Db extends Instance(
entities = Set(Entity[A](), Entity[B]()),
url = "jdbc:h2:mem:test"
)
Note that there is no requirement to follow these naming and location conventions - e.g., you can put your SORM instances in your controllers or elsewhere if you want.
In `app/controllers/Application.scala` place some controller actions utilizing SORM, e.g.:
package controllers
import play.api.mvc._
import models._
object Application extends Controller {
def index = Action {
val user = Db.save(A("test"))
Ok(user.id.toString)
}
}
This will print out a generated id of the saved `A` case class value.
Run your server using `play run` or `play start` command.
Problem
I find SORM very Interesting and promising but I cant find a way to Integrate It with play any guides?