Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...