0

I have a EJB project example that uses @PersistenceContext to conect to my database. This works fine, but since my new project isn't a EJB, I have to think of another way to to this. So I came up with something like this:

EntityManagerFactory entityFactory = Persistence.createEntityManagerFactory("avaliacaoneomind");
EntityManager em = entityFactory.createEntityManager();

But when I do this, I get this error:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named avaliacaoneomind

I know my persistence.xml is correct AND in the right place because when I use @PersistenceContext(name="avaliacaoneomind") as a test, my tables are created automaticaly in my DB. If I remove the @PersistenceContext away from my code, the tables arent created. How do I fix this error? I tried all links possible to find and none of them seems to fix my problem.

Bellow is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence>
    <!--Nome do contexto que configura o Provedor de PersistĂȘncia -->
    <persistence-unit name="avaliacaoneomind">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/avaliacaoneomind</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="both" />
        </properties>      
   </persistence-unit>
</persistence>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

0

You can achieve the entityManager through JNDI lookup

  Context initCtx = new InitialContext();
    javax.persistence.EntityManager entityManager =
            (javax.persistence.EntityManager)initCtx.lookup(
                    "java:comp/env/avaliacaoneomind"
            );
thanh ngo
  • 834
  • 5
  • 9