2016-08-16 33 views
0

我一直在這一段時間 - 希望得到一些幫助。第一個persistence.xml給出它下面的輸出。備用persistence.xml崩潰:javax.persistence.PersistenceException: No Persistence provider for EntityManager named com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU請讓我知道是否有任何我可以做出更清晰的廣告。如何讓我的項目使用容器管理的Enitymanager生命週期而不是應用程序管理的生命週期?

import javax.ejb.Stateless; 
import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.EntityTransaction; 
import javax.persistence.Persistence; 
import javax.persistence.PersistenceContext; 


@Stateless 
public class NewClass { 

@PersistenceContext(unitName ="com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU") 
static EntityManager containerManagedEntityManager; 

public static void main(String[] args) { 
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(
      "com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU"); 

    EntityManager applicationManagedEntityManager = emf.createEntityManager(); 
    System.out.println("Container managed entityManager: "+containerManagedEntityManager); 
    System.out.println( "Application managed entityManager: " +applicationManagedEntityManager); 
    } 

} 

輸出:

[EL Info]: 2016-08-16 01:51:13.395--ServerSession(33510911)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd 
[EL Info]: connection: 2016-08-16 01:51:13.535--ServerSession(33510911)--file:/Users/me/NetBeansProjects/mavenproject1/target/classes/_com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU login successful 
[EL Warning]: metamodel: 2016-08-16 01:51:13.552--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element 

Container managed entityManager: null 
Application managed entityManager: [email protected] 

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.1" 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"> 
<persistence-unit name="com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL"> 
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
<properties> 
    <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/nutrition_DB"/> 
    <property name="javax.persistence.jdbc.user" value="app"/> 
    <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/> 
    <property name="javax.persistence.jdbc.password" value="pass"/> 
    <property name="javax.persistence.schema-generation.database.action" value="create"/> 
</properties> 

替代的persistence.xml:

<persistence version="2.1" 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"> 
    <persistence-unit name="persistenceUnit" transaction-type="JTA"> 
    <jta-data-source>jdbc/dataSource</jta-data-source> 
    <class>test.domain.TestEntity</class> 
</persistence-unit> 
</persistence> 
+0

問題是什麼?它崩潰了,因爲你在備用persistence.xml中以不同方式命名PU – RafToTheK

+0

@RafToTheK。對不起,我確實修復了PU命名,但後來發生了此錯誤:javax.naming.NoInitialContextException:需要在環境或系統屬性中指定類名稱,或者作爲applet參數或在應用程序資源文件中:java.naming.factory.initial '。我的問題是如何更改我的應用程序,以便它使用容器管理的實體管理器?我正在Glassfish中運行應用程序。 – user465001

+0

當您使用@ @ PersistenceContext注入時,您的容器是託管的。我認爲你現在得到的例外是來自錯誤的部署,但我不確定(無論如何,你應該首先谷歌)。另外我在persistence.xml java:/ path/to/data/source的jta-data-source標籤中使用,不確定是否有必要 – RafToTheK

回答

0

爲了擁有一個容器管理的實體管理器,您應該使用Java Transaction API(JTA)而不是RESOURCE_LOCAL。它們之間的不同是:

  • JTA意味着持久性是通過應用服務器
  • RESOURCE_LOCAL管理意味着持久性由自己管理。

你可以看到Persistence unit as RESOURCE_LOCAL or JTA?

所以你的情況更差,我建議你做到以下幾點:

  1. 確保您在Java EE環境中,例如是Tomcat,WildFly而不是Java SE。
  2. 修改你的PU com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU,設置transaction-type="JTA"
  3. 請確保您的JTA數據源(DS)的服務器配置並啓動
  4. 說明此數據源在你的持久XML的標記jta-data-source
  5. 注入EntityManager像你所做的那樣,使用PersistenceContext進入你的java類。由於持久性由應用程序服務器管理,除非您確切知道自己在做什麼,否則不要再使用實體管理器工廠。

但是,看起來你是在Java SE環境下。在這種情況下,沒有任何Java EE容器(應用程序服務器)。因此,你不能從JTA配置中受益:(你必須使用RESOURCE_LOCAL模式並自己管理所有內容

相關問題