2017-07-20 71 views
0

引起我使用JPA發揮和遇到this.Searched完成#1無法找到解決方案。的PersistenceException:無法建立實體管理器工廠的ClassNotFound的

什麼,我想是爲localhost:9000打,HomeController的應在已經在MySQL數據庫中創建「污物」添加表「產品」。因此,這將完成我的第一個JPA程序

這裏是我的persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" 
     version="2.1"> 

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


<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <non-jta-data-source>DefaultDS</non-jta-data-source> 
    <class>models.Product</class> 
    <properties> 
     <property name="javax.persistence.jdbc.url" 
value="jdbc:mysql://localhost:3306/crud"/> 
     <property name="javax.persistence.jdbc.user" value="root"/> 
     <property name="javax.persistence.jdbc.password" 
value="toor"/> 
     <property name="javax.persistence.jdbc.driver" 
value="com.mysql.jdbc.Driver"/> 

    </properties> 

</persistence-unit> 
</persistence> 

這裏是我的application.conf

play.modules.enabled += "play.filters.csrf.CSRFModule" 


play.filters.csrf { 

    token { 
    name = "csrfToken" 
    sign = true 
    } 

    cookie { 
    name = null 
    secure = ${play.http.session.secure} 
    httpOnly = false 
    } 

    body.bufferSize = ${play.http.parser.maxMemoryBuffer} 
    bypassCorsTrustedOrigins = true 

    header { 
    name = "Csrf-Token" 
    protectHeaders { 
     Cookie = "*" 
     Authorization = "*" 
    } 
    bypassHeaders {} 
    } 

    method { 
    whiteList = ["GET", "HEAD", "OPTIONS"] 
    blackList = [] 
    } 

    contentType { 
    whiteList = [] 
    blackList = [] 
    } 

    errorHandler = null 
} 

play.crypto.secret="VRZV/ppo<t?;NKZN? 
=PE<N;Yie_G^:sxQrL544YEl[fRsrE<:hMbT;Yj<WhG`[email protected]" 


db.default.driver=com.mysql.jdbc.Driver 
db.default.url="jdbc:mysql://localhost:3306/crud" 
db.default.user=root 
db.default.pass=toor 
db.default.jndiName=DefaultDS 
jpa.default = test 

這裏是堆棧跟蹤

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PersistenceException: Unable to build entity manager factory]] 
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255) 
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182) 
    at play.core.server.AkkaHttpServer$$anonfun$$nestedInanonfun$executeHandler$1$1.applyOrElse(AkkaHttpServer.scala:252) 
    at play.core.server.AkkaHttpServer$$anonfun$$nestedInanonfun$executeHandler$1$1.applyOrElse(AkkaHttpServer.scala:251) 
    at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:412) 
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37) 
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60) 
    at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:70) 
    at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:68) 
    at scala.concurrent.impl.Promise$DefaultPromise.dispatchOrAddCallback(Promise.scala:312) 
Caused by: javax.persistence.PersistenceException: **Unable to build entity manager factory** 
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:83) 
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) 
    at controllers.HomeController.index(HomeController.java:29) 
    at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$2(Routes.scala:174) 
    at play.core.routing.HandlerInvokerFactory$$anon$3.resultCall(HandlerInvoker.scala:136) 
    at play.core.routing.HandlerInvokerFactory$$anon$3.resultCall(HandlerInvoker.scala:135) 
    at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$8$$anon$2$$anon$1.invocation(HandlerInvoker.scala:110) 
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:78) 
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [models.Product] 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:245) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.buildHibernateConfiguration(EntityManagerFactoryBuilderImpl.java:1136) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:853) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849) 
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75) 
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) 
Caused by: java.lang.ClassNotFoundException: **Could not load requested class : models.Product** 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:230) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:348) 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.buildHibernateConfiguration(EntityManagerFactoryBuilderImpl.java:1136) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:853) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425) 

Product.java(模型類)

package models; 

import java.util.ArrayList; 
import java.util.List; 
import javax.persistence.*; 

@Entity 
public class Product { 
    private static List<Product> products; 
    @Id 
    @GeneratedValue 
    private String ean; 
    @Column 
    private String name; 

    @Column 
    private String description; 

    public Product() { 
    } 

    public Product(String ean, String name, String description) { 
     this.ean = ean; 
     this.name = name; 
     this.description = description; 
    } 

    public String toString() { 
     return String.format("%s -%s", this.ean, this.name); 
    } 

    static { 
     products = new ArrayList<Product>(); 
     products.add(new Product("11111111", "Clip 1", "description 1")); 
     products.add(new Product("22222222", "Clip 2", "description 2")); 
     products.add(new Product("33333333", "Clip 3", "description 3")); 
     products.add(new Product("44444444", "Clip 4", "description 4")); 
     products.add(new Product("55555555", "Clip 5", "description 5")); 
    } 

    public static List<Product> findAll() { 
     return new ArrayList<Product>(products); 
    } 

    public static Product findByEan(String ean) { 
     for (Product candidate : products) { 
      if (candidate.ean.equals(ean)) { 
       return candidate; 
      } 
     } 
     return null; 
    } 

    public static List<Product> findByName(String term) { 
     final List<Product> results = new ArrayList<Product>(); 
     for (Product candidate : products) { 
      if (candidate.name.toLowerCase().contains(term.toLowerCase())) { 
       results.add(candidate); 
      } 
     } 
     return results; 
    } 

    public static boolean remove(Product product) { 
     return products.remove(product); 
    } 

    public void save() { 
     products.remove(findByEan(this.ean)); 
     products.add(this); 
    } 

    public static List<Product> getProducts() { 
     return products; 
    } 

    public String getEan() { 
     return ean; 
    } 

    public void setEan(String ean) { 
     this.ean = ean; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

} 

而且Homecontroller.java

package controllers; 

import models.Product; 
import org.hibernate.jpa.HibernatePersistenceProvider; 
import play.data.FormFactory; 
import play.libs.concurrent.HttpExecutionContext; 
import play.mvc.*; 

import javax.inject.Inject; 
import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 

public class HomeController extends Controller { 

    private final FormFactory formFactory; 
    private final HttpExecutionContext ec; 

    @Inject 
    public HomeController(FormFactory formFactory, HttpExecutionContext ec) { 
     this.formFactory = formFactory; 
     this.ec = ec; 
    } 

    public Result index() { 
     //Create a database and add some entries 
     Product newproduct=new Product(); 

     EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("test"); 
//Stuck at the above statment 
     EntityManager entityManager=entityManagerFactory.createEntityManager(); 
     entityManager.getTransaction().begin(); 


     newproduct.setEan("111"); 
     newproduct.setName("Lapy"); 
     newproduct.setDescription("Uska description"); 

     entityManager.persist(newproduct); 
     entityManager.getTransaction().commit(); 

     entityManager.close(); 
     entityManagerFactory.close(); 
     return ok(views.html.index.render("Database created check mysql")); 
    } 
} 

的entityManagerFactory是無法加載模型類產品。我曾在這個網站嘗試過各種解決方案,否則我不會發布。

+0

如果它不能加載一個類,則類不在一個可裝載的位置(即CLASSPATH)。既然你不提供有關什麼是在CLASSPATH然後不知道什麼魔法咒語的人都應該提供信息... –

+0

嘗試從'私人靜態列表產品刪除'static';'在'Product'類。 –

+0

@AbdullahKhan沒有工作 –

回答

0

我找到了解決辦法。最後,創建實體管理器工廠和實體管理器的代碼和開始交易應寫入控制器的構造函數(我已將其寫入Homecontroller的索引方法中)。數據庫是在mysql中創建的。

+0

神這感覺真好:) –

+0

我居然不知道爲什麼這個問題解決了嘗試這樣做,任何人都可以告訴我嗎? –

0

從你的persistence.xml

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <non-jta-data-source>DefaultDS</non-jta-data-source> 

,只是在<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">後刪除以下行提供

<class>models.Product</class> 
+0

我已經通過閱讀其他的答案,仍然沒有工作 –

相關問題