JPA with TopLink:在类路径中找不到 META-INF/persistence.xml

发布于 2024-08-02 02:58:04 字数 831 浏览 6 评论 0原文

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();

}
}

时,出现异常

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

当我尝试运行此类META-INF/persistence.xml 位于我的类路径中 。 我不知道这个异常是什么原因。

持久化库是TopLink。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

南…巷孤猫 2024-08-09 02:58:04

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

尝试将 persistence.xml 放在 src/META-INF 下。

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.

假情假意假温柔 2024-08-09 02:58:04

如果您使用 IntelliJ 或 Maven 项目结构,则需要将整个“META-INF/persistence.xml”文件放入 resources(src/resources) 文件夹中这样它将把您的 persistence.xml 文件移动到 “WEB-INF/classes/persistence.xml” 位置。

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

其他任何东西对我不起作用。

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.

物价感观 2024-08-09 02:58:04

该错误有些误导性。 XML 文件本身不应该位于类路径中; 消息中的“META-INF/persistence.xml”部分表示包含 META-INF/persistence.xml 的目录应该

如果您的硬盘驱动器具有以下

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

那么您的类路径应包含此内容

CLASSPATH=c:\libs\JPA

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

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

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

那么,你能发布你的 CLASSPATH 吗?

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?

葬心 2024-08-09 02:58:04

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

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

顾冷 2024-08-09 02:58:04

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

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]

痴梦一场 2024-08-09 02:58:04

您的 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>

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文