2013-06-01 62 views
1

運行如下因素錯誤被表明,當一試,堅持數據:麻煩配置播放2與JPA(持久性)

[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

+0

**注意**:我對Hibernate +持久性知之甚少。你應該能夠像這樣獲得'EntityManager':'EntityManager manager = JPA.em();'在Play中。然後你可以調用'manager.persist(this);'等。這適用於我,但我在'persistence.xml'中有一個額外的屬性:''。另外,可能與您的問題無關,但由於您使用的是MySQL,因此您應該將方言更改爲'org.hibernate.dialect.MySQLDialect',而不是使用'H2Dialect'。 – Aerus

回答

0

方面的名稱是不一樣的更新:例如:

jpa.update = updatePersistenceUnit

我覺得你沒有這個線的應用在應用匹配.conf和休眠文件

注意:hbm2dll中的值更新僅用於更新數據庫模式,如果它在模型中發生更改。我認爲你必須使用defaultPersistentUnit的名字,因爲在application.conf它也許是這樣的:

jpa.default = defaultPersistenUnit

然後

db.default.driver =你的驅動 db.defalut。 URL =您的網址

0

的persistence.xml文件指出以下配置:

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

<persistence-unit name="update" transaction-type="RESOURCE_LOCAL"> 

application.conf必須具有以下條目

# You can expose this datasource via JNDI if needed (Useful for JPA) 
db.default.jndiName=DefaultDS 

# JPA configuration 
# ~~~~~ 
# You can declare as many Ebean servers as you want. 
# By convention, the default server is named `default` 
jpa.default=update 

檢查您的映射,或者找到一個清晰的解釋here