0

I've been at this for a while - hoping to get some help. The first persistence.xml gives the output below it. The alternate persistence.xml crashes with: javax.persistence.PersistenceException: No Persistence provider for EntityManager named com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU Please let me know if there's anything I can ad to make this clearer.

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);
    }

}

The output:

[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

My 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>
user465001
  • 806
  • 13
  • 29
  • What is the question? It crashes, because you named the PU differently in the alternate persistence.xml – RafToTheK Aug 16 '16 at 06:03
  • @RafToTheK. Sorry, I did fix that PU naming but then I got this error `javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial`. My question is how do I change my application so that it uses a container-managed Entity Manager? I am running the application in Glassfish. – user465001 Aug 16 '16 at 06:12
  • Your em is container managed when you inject it with `@PersistenceContext` . I think the exception you get now comes from a faulty deployment, but I am not sure (anyway you should google that first). Also I use in my jta-data-source tag in the persistence.xml java:/path/to/data/source, not sure if that is necessary – RafToTheK Aug 16 '16 at 06:23
  • Check out [SDK Download](http://www.oracle.com/technetwork/java/javaee/downloads/index.html), the "Tutorials" part has a lot of great examples, which are also described in the book: [Tutorialbook](https://docs.oracle.com/javaee/7/JEETT.pdf). This book helped me a lot to understand javaEE – RafToTheK Aug 16 '16 at 06:28

1 Answers1

0

In order to have a container-managed entity manager, your should use the Java Transaction API (JTA) instead of RESOURCE_LOCAL. The different between them are :

  • JTA means the persistence is managed by the application server
  • RESOURCE_LOCAL means the persistence is managed by yourself.

You can see more difference for Persistence unit as RESOURCE_LOCAL or JTA?

So in your case, I suggest you to do the following :

  1. Make sure you're in a Java EE environment, e.g. Tomcat, WildFly and not a Java SE.
  2. Modify your PU com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU, set transaction-type="JTA"
  3. Make sure that your JTA data source (DS) is configured in the server and activated
  4. Describe this data source in your persistence XML in tag jta-data-source
  5. Inject the EntityManager into you java class using PersistenceContext as you've done. Since the persistence is managed by the application server, do not use entity manager factory anymore except you know exactly what you're doing.

However, it seems that you're under the Java SE environment. In the case, there isn't any Java EE container (application server). And therefore, you cannot benefit the JTA configuration :( You must use RESOURCE_LOCAL mode and manage everything yourself.

Community
  • 1
  • 1
Mincong Huang
  • 5,284
  • 8
  • 39
  • 62