Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA with TopLink: No META-INF/persistence.xml was found in classpath

public class LoginTest {

public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("IRCBotPU");
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();

    Login lg = new Login();
    lg.setPassword("password");
    lg.setUserName("Rocky");

    em.persist(lg);
    em.flush();

    Login st = em.find(Login.class, lg.getPassword());
    System.out.println(st);

    em.getTransaction().commit();

    em.close();
    emf.close();

}
}

I'm getting an Exception when I try to run this class

javax.persistence.PersistenceException: No Persistence provider for EntityManager named IRCBotPU:  
   No META-INF/persistence.xml was found in classpath.

META-INF/persistence.xml is in my classpath. I don't know what is the reason or this exception.

Persistence library is TopLink.

like image 736
Switch Avatar asked Aug 14 '09 17:08

Switch


2 Answers

I had the same problem, i was keeping my persistence.xml file in the WebContent/META-INF directory, while the jpa specification says:
the root of the persistence unit is the WEB-INF/classes directory; the persistence.xml file is therefore contained in the WEB-INF/classes/META-INF directory
try placing persistence.xml under src/META-INF.

like image 106
marcosbeirigo Avatar answered Oct 21 '22 14:10

marcosbeirigo


if you are using IntelliJ or a maven project structure you need to place the entire "META-INF/persistence.xml" file in the in resources(src/resources) folder so that it will move your persistence.xml file into "WEB-INF/classes/persistence.xml" location.

if you are using eclipse or something else makes the changes accordingly so that it will move the file to WEB-INF/classes/persistence.xml

anything else did not work for me.

like image 33
Aniruddha Das Avatar answered Oct 21 '22 13:10

Aniruddha Das