![](/img/trans.png)
[英]no persistence provider for EntityManager named persistence.xml
[英]Persistence.xml settings for hibernate and mysql says “No Persistence provider for EntityManager”
我正在嘗試使用JPA和休眠模式來實現一個簡單的ORM程序。 我希望將其編寫為盡可能可移植,以便在提供其他持久性提供程序時,僅需更改persistence.xml文件。 以下是我擁有的persistence.xml文件。
<persistence xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="JPA_Project" transaction-type="RESOURCE_LOCAL">
<description>
Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tutorial.prj.Book</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpatestdb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="123" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
上面的persistence.xml文件正確嗎? 當我通過Eclipse運行程序時,它說Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named JPA_Project
我已經將休眠jar和mysql連接器添加到了構建路徑。 這是什么問題? 我還看到了以下在Internet上使用的不同類型的persistence.xml文件。
<persistence-unit name="my_schema">
... classes ...
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://URL:3306/my_schema"></property>
<property name="hibernate.connection.username" value="my_user"></property>
<property name="hibernate.connection.password" value="my_pass"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.connection.nombre" value="my_schema" />
</properties>
</persistence-unit>
這里的屬性名稱與我使用的屬性名稱不同,使用這些屬性名稱的背后原因是什么? 哪個應該是正確的persistence.xml文件,以便於移植。
請注意,這不是Web應用程序。 是否由於不是Web應用程序而出現此問題? 休眠可以在本地桌面應用程序中使用嗎? 我的persistence.xml文件存儲在\\JPA_Project\\src\\META-INF
文件夾中。
您需要添加hiberntare-entitymanager
jar包。
如果您正在使用maven,則需要添加依賴項:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.