我有一个标准的 Java 应用程序(不是 Java EE),我正在尝试学习 JPA。问题是当我运行项目时,控制台输出:
1218 [main] INFO org.hibernate.ejb.Ejb3Configuration - HHH000318: Could not find any META-INF/persistence.xml file in the classpath
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named entities
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
at com.svprdga.test.App.main(App.java:26)
所以我认为它没有找到我的 persistence.xml 文件,我四处搜索,发现涉及 WEB-INF 等 Java EE 文件夹的修复,但这不是我的情况,我也尝试了另一种解决方案,但问题仍然存在那里。
我在 META-INF 中有 persistence.xml,在 /src 下,它的内容是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="entities" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.svprdga.test.database.entity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/hibtest"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.password" value=""/>
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
谢谢你的帮助。