我是 JPA 和 Hibernate 的新手。在阅读了一些在线资料后,我现在了解了 Hibernate 是什么以及它如何与 JPA 一起使用。
现在,我正在尝试运行 JPA & Hibernate tutorial .我已经完成了他们在本教程中提到的所有内容。
我没有 Oracle DB,只有 MySQL。所以我使用我对 JPA 和 Hibernate 的理解对 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_1_0.xsd" version="1.0">
<persistence-unit name="customerManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Customer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
但我似乎没有得到他们描述的输出。它给了我:
Customer id before creation:null
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at CustomerDAO.create(CustomerDAO.java:8)
at CustomerDAO.main(CustomerDAO.java:22)
我们将不胜感激。
更新:
我已完成要求进行的更改。但是,仍然出现 asme 错误行!!!
他们在那个教程中没有提到任何关于 orm.xml 的内容。可能是问题的原因!!!
最佳答案
只是为了完整性。还有另一种情况导致这个错误:
missing META-INF/services/javax.persistence.spi.PersistenceProvider file.
对于 Hibernate,它位于 hibernate-entitymanager-XXX.jar
中,因此,如果 hibernate-entitymanager-XXX.jar
不在您的类路径中,您将得到这个错误也是。
这个错误信息非常具有误导性,我花了好几个小时才把它改正。
见 JPA 2.0 using Hibernate as provider - Exception: No Persistence provider for EntityManager .
关于javax.persistence.PersistenceException : No Persistence provider for EntityManager named customerManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3739387/