Java 带有 TopLink 的 JPA:在类路径中找不到 META-INF/persistence.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1279060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:43:56  来源:igfitidea点击:

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

javajpapersistenceclasspathtoplink

提问by Switch

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.

META-INF/persistence.xml 在我的类路径中。我不知道是什么原因或这个例外。

Persistence library is TopLink.

持久性库是 TopLink。

回答by ChssPly76

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

persistence.xml不应在您的类路径中;包含persistence.xml在其 META-INF 文件夹中的JAR 文件应该。

回答by ChssPly76

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

您的 META-INF/persistence.xml 文件应如下所示:

// <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".

您的persistence.xml 中的persistence-unit 的名称属性与您传递给Persistence.createEntityManagerFactory 方法的值不匹配。确保您的持久性单元名称设置为“IRCBotPU”。

回答by Kelly S. French

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

这个错误有点误导。XML文件本身应该不会在类路径; 消息中说“META-INF/persistence.xml”的部分意味着包含 META-INF/persistence.xml 的目录应该是。

If your hard drive had the following

如果您的硬盘有以下情况

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

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

如果 META-INF\Persistence.xml 包含在 foo.jar 中,假设 META-INF/Persistence.xml 位于 jar 的根文件夹中,您的类路径应该有这个

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.

这可能看起来很明显或多余,但我的目标是确保我们将苹果与苹果进行比较,并且 CLASSPATH 以及类加载可能是一个需要处理的问题。

So, can you post your CLASSPATH?

那么,您可以发布您的 CLASSPATH 吗?

回答by marcosbeirigo

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.

我遇到了同样的问题,我将我的 persistence.xml 文件保存在 WebContent/META-INF 目录中,而 jpa 规范说:
持久性单元的根目录是 WEB-INF/classes 目录;因此,persistence.xml 文件包含在 WEB-INF/classes/META-INF 目录中,
尝试将 persistence.xml 放在 src/META-INF 下。

回答by nu1silva

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]

我有一个网络应用程序,以下工作正常。我所做的是将带有 persistence.xml 的 META-INF 目录添加到 src/main/resources 位置 [Maven 项目]

回答by Wai Hein

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

我在 src 下创建了一个名为 META-INF 的文件夹,它可以工作。“marcosbeirigo”已经回答了。我不知道为什么我必须把那个 persistance.xml 放在那里。我把它放在 WebContent/META-INF 下并没有工作

回答by Aniruddha Das

if you are using IntelliJor 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.xmlfile into "WEB-INF/classes/persistence.xml"location.

如果您正在使用IntelliJMaven 项目结构,您需要将整个"META-INF/persistence.xml"文件放在资源(src/resources)文件夹中,以便它将您的persistence.xml文件移动到"WEB-INF/classes/persistence.xml"位置。

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

如果您正在使用eclipse或其他东西进行相应的更改,以便它将文件移动到WEB-INF/classes/persistence.xml

anything else did not work for me.

其他任何东西都不适合我。