I was trying to build a Java project using Play framework2.0. I tried to configure JPA in the play like this:
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/tt"
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"
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
)
}
app/models/user.java
package models;
import javax.persistence.*;
import play.data.validation.Constraints.Required;
import play.db.jpa.JPA;
@Entity
public class user {
@Id
public Long id;
@Required
public String name;
public static user findById(Long id) {
return JPA.em().find(user.class, id);
}
}
app/controllers/application.java
package controllers;
import models.*;
import play.*;
import play.db.jpa.Transactional;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
@Transactional
public static Result index() {
user u = user.findById((long)1);
return ok(index.render(u.name));
}
}
Then, in the working directory I type "play " then in the console I type"run", and the errors in the stack are as follows:
[info] play - datasource [jdbc:mysql://localhost:3306/tt] bound to JNDI as Defau
ltDS
[info] play - database [default] connected at jdbc:mysql://localhost:3306/tt
[error] application -
! @6akap0ohi - Internal server error, for request [GET /] ->
play.api.UnexpectedException: Unexpected exception [PersistenceException: [Persi
stenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$3$$anon
fun$1.apply(ApplicationProvider.scala:134) ~[play_2.9.1.jar:2.0]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$3$$anon
fun$1.apply(ApplicationProvider.scala:112) ~[play_2.9.1.jar:2.0]
at scala.Option.map(Option.scala:133) ~[scala-library.jar:0.11.2]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$3.apply
(ApplicationProvider.scala:112) ~[play_2.9.1.jar:2.0]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$3.apply
(ApplicationProvider.scala:110) ~[play_2.9.1.jar:2.0]
at scala.Either$RightProjection.flatMap(Either.scala:277) ~[scala-librar
y.jar:0.11.2]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPers
istenceUnit] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Con
figuration.java:915) ~[hibernate-entitymanager-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Con
figuration.java:890) ~[hibernate-entitymanager-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(Hib
ernatePersistence.java:57) ~[hibernate-entitymanager-4.1.2.Final.jar:4.1.2.Final
]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.
java:63) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.
java:47) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play_2.9.1.jar:2.0
]
Caused by: org.hibernate.HibernateException: Dialect class not found: org.hibern
ate.dialect.MYSQL5Dialect
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constr
uctDialect(DialectFactoryImpl.java:76) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Fi
nal]
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildD
ialect(DialectFactoryImpl.java:64) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcSer
vicesImpl.java:146) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureS
ervice(StandardServiceRegistryImpl.java:75) ~[hibernate-core-4.1.2.Final.jar:4.1
.2.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initialize
Service(AbstractServiceRegistryImpl.java:159) ~[hibernate-core-4.1.2.Final.jar:4
.1.2.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService
(AbstractServiceRegistryImpl.java:131) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Fi
nal]
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable
to load class [org.hibernate.dialect.MYSQL5Dialect]
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.cl
assForName(ClassLoaderServiceImpl.java:141) ~[hibernate-core-4.1.2.Final.jar:4.1
.2.Final]
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constr
uctDialect(DialectFactoryImpl.java:73) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Fi
nal]
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildD
ialect(DialectFactoryImpl.java:64) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcSer
vicesImpl.java:146) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureS
ervice(StandardServiceRegistryImpl.java:75) ~[hibernate-core-4.1.2.Final.jar:4.1
.2.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initialize
Service(AbstractServiceRegistryImpl.java:159) ~[hibernate-core-4.1.2.Final.jar:4
.1.2.Final]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : or
g.hibernate.dialect.MYSQL5Dialect
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.
findClass(ClassLoaderServiceImpl.java:99) ~[hibernate-core-4.1.2.Final.jar:4.1.2
.Final]
at java.lang.ClassLoader.loadClass(ClassLoader.java:307) ~[na:1.6.0_23]
at java.lang.ClassLoader.loadClass(ClassLoader.java:248) ~[na:1.6.0_23]
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.cl
assForName(ClassLoaderServiceImpl.java:138) ~[hibernate-core-4.1.2.Final.jar:4.1
.2.Final]
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constr
uctDialect(DialectFactoryImpl.java:73) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Fi
nal]
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildD
ialect(DialectFactoryImpl.java:64) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]