2016-08-16 58 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模式并自己管理所有内容

相关问题