gpt4 book ai didi

java - 配置 Play 2 以使用 JPA 运行时出现问题(持久性)

转载 作者:行者123 更新时间:2023-12-01 04:39:10 24 4
gpt4 key购买 nike

尝试保存数据时出现以下错误:

[PersistenceException: [PersistenceUnit: update] class or package not found]
In C:\Users\Johann\Desktop\SODET\play-2.1.1\todolist\app\models\Stream.java at line 32.



32 EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("update");

这是Stream.java(模型)

package models;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.Persistence;

import com.restfb.Facebook;

@Entity
public class Stream {
@Facebook
@Id
public String post_id;

@Facebook
public String created_time;

@Facebook
public String message;

@Facebook
public String permalink;

@Override
public String toString() {
return String.format("%s, %s, %s, %s", created_time, message, permalink, post_id);
}

public void save(){
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("update");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(this);
entityManager.getTransaction().commit();
entityManager.close();
}

}

persistence.xml 文件位于conf/META-INF 目录中:

<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="update" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<non-jta-data-source>DefaultDS</non-jta-data-source>

<class>models.Stream</class>
<class>models.FacebookUser</class>
<class>models.Comment</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
</properties>
</persistence-unit>

</persistence>

这是Build.scala

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

val appName = "todolist"
val appVersion = "1.0-SNAPSHOT"

val appDependencies = Seq(
// Add your project dependencies here,
"com.restfb" % "restfb" % "1.6.11",
"mysql" % "mysql-connector-java" % "5.1.25",
"org.hibernate" % "hibernate-entitymanager" % "4.2.1.Final",
javaCore,
javaJdbc,
javaJpa
)

val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
)

}

我查看了文档,但没有发现任何相关内容......

提前致谢!

最佳答案

注意您的 application.conf 中的名称与 update 不同:示例:

jpa.update=updatePersistenceUnit

我认为您没有应用程序必须在 application.conf 和 hibernate 文件中匹配的这一行

注意:hbm2dll 中的值更新仅用于在模型发生更改时更新数据库架构。我认为你必须使用 defaultPersistentUnit 名称,因为在 application.conf 中它可能是这样的:

jpa.default=defaultPersistenUnit

然后

db.default.driver=您的驱动程序db.defalut.url=您的网址

关于java - 配置 Play 2 以使用 JPA 运行时出现问题(持久性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16875955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com