JPA with TopLink:在类路径中找不到 META-INF/persistence.xml
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我遇到了同样的问题,我将 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.
如果您使用 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 yourpersistence.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 toWEB-INF/classes/persistence.xml
anything else did not work for me.
该错误有些误导性。 XML 文件本身不应该位于类路径中; 消息中的“META-INF/persistence.xml”部分表示包含 META-INF/persistence.xml 的目录应该。
如果您的硬盘驱动器具有以下
C:\libs\JPA\META-INF\Persistence.xml
那么您的类路径应包含此内容
如果 META-INF\Persistence.xml 包含在 foo.jar 中,假设 META-INF/Persistence.xml位于 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
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
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?
persistence.xml
不应该在您的类路径中; JAR 文件应在其 META-INF 文件夹中包含persistence.xml
。persistence.xml
should not be in your classpath; JAR file that containspersistence.xml
in its META-INF folder should.我有一个网络应用程序,以下工作正常。 我所做的是将带有 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]
您的 META-INF/persistence.xml 文件应如下所示:
persistence.xml 中的持久单元名称属性与您传递到 Persistence.createEntityManagerFactory 方法中的值不匹配。 确保您的持久性单元名称设置为“IRCBotPU”。
Your META-INF/persistence.xml file should look something like this:
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