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

7

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

Servant answered 14/8, 2009 at 17:20 Comment(1)
@oxbow_lakes - You could have fixed it :-) I just did.Willaims
B
23

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.

Berezina answered 11/3, 2010 at 0:47 Comment(1)
in the eclipse following worked for me: src/META-INF/persistence.xmlThereafter
W
3

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.

Wishywashy answered 4/4, 2016 at 21:52 Comment(0)
L
2

The error is somewhat misleading. the XML file itself should not be in the classpath; the part of the message saying "META-INF/persistence.xml" means that the directory containing META-INF/persistence.xml should be.

If your hard drive had the following

C:\libs\JPA\META-INF\Persistence.xml

then your classpath should include this

CLASSPATH=c:\libs\JPA

If META-INF\Persistence.xml were contained in foo.jar, assuming META-INF/Persistence.xml were located on the root folder of the jar, your classpath should have this

CLASSPATH=C:\<path to jar>\foo.jar

This may seem obvious or redundant but my goal is to make sure we're comparing apples to apples and the CLASSPATH, along with classloading, can be a bugger to deal with.

So, can you post your CLASSPATH?

Lanugo answered 14/8, 2009 at 20:8 Comment(2)
My classpath is set to the exact way that you have mentioned above.But it still won't work.Servant
Is the Jar that contains the implementation of "IRCBotPU" in the classpath?Lanugo
W
1

persistence.xml should not be in your classpath; JAR file that contains persistence.xml in its META-INF folder should.

Willaims answered 14/8, 2009 at 17:27 Comment(2)
What is the name of the JAR that contains a META-INF/persistence.xml? Is said JAR in your apps CLASSPATH?Lanugo
No it's not a JAR file I'm using NetBeans 6.0 so it just creates the package META-INF and inside it persistence.xml.Servant
S
1

i have a web application and the following worked fine. What i did was add the META-INF directory with the persistence.xml into the src/main/resources location [Maven project]

Splatter answered 6/3, 2012 at 8:15 Comment(0)
R
0

Your META-INF/persistence.xml file should look something like this:

// <persistence>
//   <persistence-unit name="IRCBotPU">
//     <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
//     <!-- All persistence classes must be listed -->
//     <class>entity.Customer</class>
//     <class>entity.Order</class>
//     <class>entity.Item</class>
//     <properties>
//       <!-- Provider-specific connection properties -->
//       <property name="toplink.jdbc.driver" value="<database driver>"/>
//       <property name="toplink.jdbc.url" value="<database url>"/>
//       <property name="toplink.jdbc.user" value="<user>"/>
//       <property name="toplink.jdbc.password" value="<password>"/>
//       <!-- Provider-specific settings -->
//       <property name="toplink.logging.level" value="INFO"/>
//     </properties>
//   </persistence-unit>
// </persistence>

Your persistence-unit's name attribute in your persistence.xml doesn't match the value you're passing into the Persistence.createEntityManagerFactory method. Make sure that your persistence-unit name is set to "IRCBotPU".

Rub answered 14/8, 2009 at 17:49 Comment(0)
P
0

I created a folder callled META-INF under src and it works. "marcosbeirigo" answered it already. I do not know why I have to put that persistance.xml in there though. I put it under WebContent/META-INF and did not work

Pretrice answered 6/12, 2013 at 16:56 Comment(1)
Because src is on the classpath, that is why it worked.Generator

© 2022 - 2024 — McMap. All rights reserved.