我面临着将eclipselink集成到play框架中的一些问题。当我键入运行时,服务器启动,我可以连接到我的应用程序,没有任何错误。
但是:之后当我(作为一个例子)强制重新编译并稍加改动时,应用程序就会出错(在em.persist上):
play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: Object: model.Car@1a5f139c is not a known entity type.]]
我做了什么(保持简单)汽车模型:
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;
}
}
我的build.sbt看起来像这样:
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(部分地):
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
在conf.META-INFO中解析我的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">
<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>
现在执行调用的控制器:
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."));
}
}
提前致谢!
答案 0 :(得分:1)
自己解决了这个问题:
关键是使用@Transactional进行注释。但是在build.sbt必须像这样修改之前
"org.eclipse.persistence" % "eclipselink" % "2.5.1",
"mysql" % "mysql-connector-java" % "5.1.18",
javaJdbc,
javaJpa
在游戏机中你必须“重新加载”。并且只有“eclipse”再次导入项目(至少我的日食不喜欢它)你可以导入
import play.db.jpa.Transactional;
结束
JPA.em()
你可以获得实体经理。