javax.persistence.PersistenceException:没有名为 customerManager 的 EntityManager 的持久性提供程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3739387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager
提问by
I am new to JPA & Hibernate. After reading some online materials I now understand what Hibernate is and how it can be used with JPA.
我是 JPA 和 Hibernate 的新手。在阅读了一些在线材料后,我现在了解了 Hibernate 是什么以及它如何与 JPA 一起使用。
Now, I am trying to run this JPA & Hibernate tutorial. I've done everything they mention in this tutorial.
现在,我正在尝试运行此JPA 和 Hibernate 教程。我已经完成了他们在本教程中提到的所有内容。
I don't have Oracle DB, only MySQL. So I made some changes to persistence.xml
using my understanding of JPA & Hibernate (I don't know if it's correct or not... Seems to me it is.)
我没有 Oracle DB,只有 MySQL。所以我persistence.xml
使用我对 JPA 和 Hibernate 的理解做了一些改变(我不知道它是否正确......在我看来是这样。)
Here is my 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>
But I don't seem to get the output they describe. It's giving me:
但我似乎没有得到他们描述的输出。它给了我:
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)
Any suggestions will be appreciated.
任何建议将不胜感激。
Update:
更新:
I have made the changes that are asked to done. But, still getting the asme error lines!!!
我已经进行了要求完成的更改。但是,仍然收到 asme 错误行!!!
They didnt mentioned anything about orm.xmlin that tutorial. may it be a problem causer!!!
他们在那个教程中没有提到任何关于orm.xml的内容。可能是问题原因!!!
采纳答案by Pascal Thivent
Your persistence.xml
is not valid and the EntityManagerFactory
can't get created. It should be:
您persistence.xml
的无效且EntityManagerFactory
无法创建。它应该是:
<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>
(Note how the <property>
elements are closed, they shouldn't be nested)
(注意<property>
元素是如何关闭的,它们不应该嵌套)
Update:I went through the tutorial and you will also have to change the Id
generation strategy when using MySQL (as MySQL doesn't support sequences). I suggest using the AUTO
strategy (defaults to IDENTITY with MySQL). To do so, remove the SequenceGenerator
annotation and change the code like this:
更新:我完成了教程,您还必须Id
在使用 MySQL 时更改生成策略(因为 MySQL 不支持序列)。我建议使用该AUTO
策略(MySQL 默认为 IDENTITY)。为此,请删除SequenceGenerator
注释并像这样更改代码:
@Entity
@Table(name="TAB_CUSTOMER")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="CUSTOMER_ID", precision=0)
private Long customerId = null;
...
}
This should help.
这应该有帮助。
PS: you should also provide a log4j.properties
as suggested.
PS:您还应该log4j.properties
按照建议提供。
回答by Nicolas Landier
A bit too late but I got the same issue and fixed it switching schemalocationinto schemaLocationin the persistence.xmlfile (line 1).
有点晚了,但我遇到了同样的问题,并修复了它在persistence.xml文件(第1 行)中将schemalocation切换到schemaLocation 的问题。
回答by Vivek
I was facing the same issue. I realised that I was using the Wrong provider class in persistence.xml
我面临着同样的问题。我意识到我在persistence.xml中使用了错误的提供者类
For Hibernate it should be
对于 Hibernate,它应该是
<provider>org.hibernate.ejb.HibernatePersistence</provider>
And for EclipseLink it should be
对于 EclipseLink,它应该是
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
回答by turtledove
Just for completeness. There is another situation causing this error:
只是为了完整性。还有另一种情况会导致此错误:
missing META-INF/services/javax.persistence.spi.PersistenceProvider file.
缺少 META-INF/services/javax.persistence.spi.PersistenceProvider 文件。
For Hibernate, it's located in hibernate-entitymanager-XXX.jar
, so, if hibernate-entitymanager-XXX.jar
is not in your classpath, you will got this error too.
对于 Hibernate,它位于 中hibernate-entitymanager-XXX.jar
,因此,如果hibernate-entitymanager-XXX.jar
不在您的类路径中,您也会收到此错误。
This error message is so misleading, and it costs me hours to get it correct.
这个错误信息非常具有误导性,我花了好几个小时才能把它改正。
See JPA 2.0 using Hibernate as provider - Exception: No Persistence provider for EntityManager.
请参阅JPA 2.0 using Hibernate as provider - Exception: No Persistence provider for EntityManager。
回答by Victor Ma
my experience tells me that missing persistence.xml,will generate the same exception too.
我的经验告诉我,缺少persistence.xml,也会产生同样的异常。
i caught the same error msg today when i tried to run a jar package packed by ant.
今天,当我尝试运行由 ant 打包的 jar 包时,我遇到了同样的错误消息。
when i used jar tvf to check the content of the jar file, i realized that "ant" forgot to pack the persistnece.xml for me.
当我使用 jar tvf 检查 jar 文件的内容时,我意识到“ant”忘记为我打包 persistnece.xml。
after I manually repacked the jar file ,the error msg disappered.
在我手动重新打包 jar 文件后,错误消息消失了。
so i believe maybe you should try simplely putting META-INF under src directory and placing your persistence.xml there.
所以我相信也许您应该尝试简单地将 META-INF 放在 src 目录下并将您的 persistence.xml 放在那里。
回答by Dan Torrey
I had the same problem today. My persistence.xml was in the wrong location. I had to put it in the following path:
我今天遇到了同样的问题。我的 persistence.xml 位于错误的位置。我不得不把它放在以下路径中:
project/src/main/resources/META-INF/persistence.xml
回答by Raja
I have seen this error , for me the issue was there was a space in the absolute path of the persistance.xml , removal of the same helped me.
我已经看到了这个错误,对我来说问题是在 persistance.xml 的绝对路径中有一个空格,删除它对我有帮助。
回答by Shirshendu Shekhar Das
I was also facing the same issue when I was trying to get JPA entity manager configured in Tomcat 8. First I has an issue with the SystemException class not being found and hence the entityManagerFactory was not being created. I removed the hibernate entity manager dependency and then my entityManagerFactory was not able to lookup for the persistence provider. After going thru a lot of research and time got to know that hibernate entity manager is must to lookup for some configuration. Then put back the entity manager jar and then added JTA Api as a dependency and it worked fine.
当我尝试在 Tomcat 8 中配置 JPA 实体管理器时,我也遇到了同样的问题。首先,我遇到了找不到 SystemException 类的问题,因此没有创建 entityManagerFactory。我删除了休眠实体管理器依赖项,然后我的 entityManagerFactory 无法查找持久性提供程序。经过大量研究和时间后,我了解到休眠实体管理器必须查找一些配置。然后放回实体管理器 jar,然后添加 JTA Api 作为依赖项,它工作正常。
回答by Justas
If you use Hibernate 5.2.10.Final, you should change
如果你使用 Hibernate 5.2.10.Final,你应该改变
<provider>org.hibernate.ejb.HibernatePersistence</provider>
to
到
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
in your persistence.xml
在你的persistence.xml
According to Hibernate 5.2.2: No Persistence provider for EntityManager
根据Hibernate 5.2.2: No Persistence provider for EntityManager
回答by idarwin
If you are using Maven you may have bothsrc/{main,test}/resources/META-INF/persistence.xml
. This is a common setup: test your JPA code with h2 or Derby and deploy it with PostgreSQL or some other full DBMS. If you're using this pattern, do make sure the two files have differentunit names, else some versions of the Persistence
class will try to load BOTH (because of course your test-time CLASSPATH includes both classes and test-classes); this will cause conflicting definitions of the persistence unit, resulting in the dreaded annoying message that we all hate so much!
如果您正在使用Maven你可能有两个src/{main,test}/resources/META-INF/persistence.xml
。这是一个常见的设置:使用 h2 或 Derby 测试您的 JPA 代码,并使用 PostgreSQL 或其他一些完整的 DBMS 部署它。如果您使用此模式,请确保两个文件具有不同的单元名称,否则Persistence
该类的某些版本将尝试加载两者(因为当然您的测试时 CLASSPATH 包括类和测试类);这将导致持久性单元的定义发生冲突,从而导致我们都非常讨厌的可怕的烦人信息!
Worse: this may "work" with some older versions of e.g., Hibernate, but fail with current versions. Worth getting it right anyway...
更糟糕的是:这可能与一些旧版本的 Hibernate 一起“工作”,但在当前版本中失败。无论如何都值得正确...