Reputation: 566
Im faced with some problems for the integration of eclipselink into the play framework. When I type run the server starts and I can connect to my application without any error.
But: Afterwards when I (as an example) force a recompile with a little change then the application breaks with an error (on em.persist):
play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: Object: model.Car@1a5f139c is not a known entity type.]]
What I did(to keep it simple) a car model:
package model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "car")
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int carId;
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
My build.sbt looks like this:
name := "WebTest"
version := "1.0-SNAPSHOT"
libraryDependencies ++= Seq(
"org.eclipse.persistence" % "eclipselink" % "2.5.1",
"mysql" % "mysql-connector-java" % "5.1.18",
javaJdbc,
javaEbean,
cache
)
play.Project.playJavaSettings
application.conf(partially):
db.default.jndiName=DefaultDS
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/playjpa?characterEncoding=UTF-8"
db.default.user=xxxx
db.default.password=xxxx
jpa.default=defaultPersistenceUnit
Ans my persistence.xml in conf.META-INFO(drop create was in there before):
<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">
<class>model.Car</class>
<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>
And now the controller which executes the call:
public class Application extends Controller {
private static final String PERSISTENCE_UNIT_NAME = "defaultPersistenceUnit";
private static EntityManagerFactory factory;
public static Result index() {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Car car = new Car();
car.setName("Supercar");
em.persist(car);
em.getTransaction().commit();
em.close();
return ok(index.render("Your new application is ready."));
}
}
Thanks in advance!
Upvotes: 1
Views: 327
Reputation: 566
Solved the problem myself:
The key is to annotate with @Transactional. But before the build.sbt has to be modified like this
"org.eclipse.persistence" % "eclipselink" % "2.5.1",
"mysql" % "mysql-connector-java" % "5.1.18",
javaJdbc,
javaJpa
And in the play console you have to "reload". And only if "eclipse" and import the project again(at least my eclipse didnt like it) you can import the
import play.db.jpa.Transactional;
And over
JPA.em()
you can get the entitymanager.
Upvotes: 1