Using standard JPA annotations,
@Entity denotes that the
Group class is persistent, while
@Table(name="groups") alters the name of the table when mapping the class to SQL. We need to do this because group is a reserved name in SQL -- MariaDB won't like it if we try to
select * from group.
We also annotate the
id field using
@GeneratedValue, because we made that an auto-incremented field in our DB table. In keeping with the JPA convention, the field
name will be persisted without any additional annotations.
Configure the ORM
Next, we want to enable the EBean plugin that came with Play, and we do that in the
project/plugins.sbt file. Listing 4 shows the line that you want to ensure is uncommented in
Listing 4. Adding the EBean plugin dependency
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")
Logging levels in Play
While you are looking at
plugins.sbt, note that you can change logging levels for your app there. For example, if you want debug-level logging, you can add the line:
logLevel := Level.Debug.
We've enabled EBean as a plugin but we still have to tell Play to use it in our project. We do that by adding it to the build definition file,
build.sbt. You can see how this is done in Listing 5. Notice the project name is
microPlay (comment 1) and that the name is referenced in the line commented as 2.
Listing 5. Modifications to build.sbt, including enabling PlayEbean
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0") name := "microPlay" // 1 version := "1.0-SNAPSHOT" lazy val microPlay = (project in file(".")).enablePlugins(PlayJava, PlayEbean) // 2 scalaVersion := "2.11.6" libraryDependencies ++= Seq( javaJdbc, cache, javaWs, "mysql" % "mysql-connector-java" % "5.1.18" ) // 3 // Play provides two styles of routers, one expects its actions to be injected, the // other, legacy style, accesses its actions statically. routesGenerator := InjectedRoutesGenerator //fork in run := true //4
In line 2 we just add
PlayEbean to our
enablePlugins call -- simple enough. Also in
build.sbt (comment 3), we add the MySQL driver as a dependency (
mysql" % "mysql-connector-java" % "5.1.18). This is done by adding the dependency to the
libraryDependency Sequence, which is just a Scala collection. The format uses the same semantics but different syntax than a Maven dependency; notice how the group ID, artifact ID, and version are separated by percentage signs.
If your DB driver is different, simply go to Maven Repo and search for your database, then add it in place of the MySQL in Listing 5.
Finally, in Listing 5, notice the line commented as 4. This line isn't normally commented when Play builds your app, but I observed very poor compile and load times when it was enabled, along with numerous timeouts, so your mileage may vary. The issue is addressed in this StackOverflow question.
Sign up for CIO Asia eNewsletters.