如何让我的项目使用容器管理的Enitymanager生命周期而不是应用程序管理的生命周期?

时间:2016-08-16 05:06:29

标签: jpa java-ee

我已经有一段时间了 - 希望得到一些帮助。第一个persistence.xml给出了它下面的输出。备用persistence.xml崩溃:javax.persistence.PersistenceException: No Persistence provider for EntityManager named com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU请告诉我是否有任何我可以做的广告来使这一点更清晰。

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: org.eclipse.persistence.internal.jpa.EntityManagerImpl@6f139fc9

我的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>

Alternate 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 个答案:

没有答案