How do I use the SORM framework with the Play Framework?
- Install Play> = 2.1.0.
- Create a Project Using the Replay Guides
-
Add the appropriate SORM and selected database dependencies to the generated one
project/Build.scala
, for example:val appDependencies = Seq( "org.sorm-framework" % "sorm" % "0.3.8", "com.h2database" % "h2" % "1.3.168" )
-
In the same file, make sure your project depends on the same version of Scala that SORM depends on (for SORM 0.3.8 it Scala 2.10.1):
val main = play.Project(appName, appVersion, appDependencies).settings( scalaVersion := "2.10.1" )
If you skip this step, you may encounter this problem .
-
In
app/models/package.scala
place all your case classes and the SORM instance declaration, for example: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 need to follow these naming and location conventions - for example, you can put your SORM instances in your controllers or elsewhere if you like.
-
In
app/controllers/Application.scala
put some controller actions using SORM like: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 output the generated ID of the stored case class value
A
. -
Start the server with the command
play run
orplay start
.
source to share
Play has been updated to use the new build file format
Link: Build.scala is not generated in the game
You can continue with the build.sbt file for more
ForEx:
libraryDependencies ++= Seq(
jdbc,
cache,
"org.sorm-framework" % "sorm" % "0.3.8",
ws,
specs2 % Test
)
For new use:
source to share