我在将 eclipselink 集成到 play 框架中遇到了一些问题。当我输入 run 时,服务器启动,我可以连接到我的应用程序而不会出现任何错误。
但是:之后当我(作为示例)强制重新编译并进行一点更改时,应用程序会因错误而中断(在 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
Ans 我的 persistence.xml 在 conf.META-INFO 中(drop create 之前就在那里):
<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>
现在执行调用的 Controller :
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."));
}
}
提前致谢!
最佳答案
我自己解决了这个问题:
关键是用@Transactional进行注解。但在build.sbt之前必须像这样修改
"org.eclipse.persistence" % "eclipselink" % "2.5.1",
"mysql" % "mysql-connector-java" % "5.1.18",
javaJdbc,
javaJpa
在游戏控制台中,您必须“重新加载”。并且只有“eclipse”并再次导入项目(至少我的 eclipse 不喜欢它),您才能导入
import play.db.jpa.Transactional;
还有
JPA.em()
您可以获得实体管理器。
关于java - 在更改和重新加载时 Play Framework 并出现 eclipselink 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23651047/