Play Framework 2 – JPA – EclipseLink Setup

How to use EclipseLink as JPA in Play Framework 2. Setup and Configuration.

1. Fisrt create a new Play project.

$> play new EclipseLink-Demo

2. Change Build.scala, adding dependencies

$> cd EclipseLink-Demo/project
$> vi Build.scala

Build.scala

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

  val appName         = "EclipseLink-Demo"
  val appVersion      = "1.0-SNAPSHOT"

  val appDependencies = Seq(
    "mysql" % "mysql-connector-java" % "5.1.21",
	"org.eclipse.persistence" % "eclipselink" % "2.5.0",
    javaCore,
    javaJdbc,
    javaJpa
  )

  val main = play.Project(appName, appVersion, appDependencies).settings(
    // Add your own project settings here      
  )
}

3. Adding META-INF/persistence.xml in folder /conf

$> cd ../conf
$> mkdir META-INF
$> cd META-INF
$> vi persistence.xml

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
		
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
		
        <properties>
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
        </properties>
    </persistence-unit>
</persistence>

4. Change configuration in application.conf

$> cd ..
$> vi application.conf

application.conf

...

# Database configuration
# ~~~~~ 
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/eclipselink_demo"
db.default.user=root
db.default.password=123456

#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS

# Extra Addition
# ~~~~~ 
# The name of the persistence-unit you gave in persistence.xml
jpa.default=defaultPersistenceUnit

...
Advertisements

2 thoughts on “Play Framework 2 – JPA – EclipseLink Setup

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s