Next Design ltd. 2017.4.4

Play framework 2.0 Java

JPA MySQLの設定例

2012.4.15

■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/test"
db.default.user=***
db.default.password=******
#
# You can expose this datasource via JNDI if needed (Useful for JPA)
# db.default.jndiName=DefaultDS
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit

■conf/META-INF/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.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source> 
<properties> 
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/> 
</properties> 
</persistence-unit> 
</persistence> 

■project/Build.scala

import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
    val appName         = "appjava"
    val appVersion      = "1.0-SNAPSHOT"
    val appDependencies = Seq(
      // Add your project dependencies here,
        "mysql" % "mysql-connector-java" % "5.1.19",
        "org.hibernate" % "hibernate-entitymanager" % "4.1.2.Final"
    )
    val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
      // Add your own project settings here      
    )
}