Java 没有命名 EntityManager 的持久性提供程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1158159/
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
No Persistence provider for EntityManager named
提问by Robert A Henru
I have my persistence.xml
with the same name using TopLink
under the META-INF
directory.
Then, I have my code calling it with:
我在目录下persistence.xml
使用了同名。然后,我有我的代码调用它:TopLink
META-INF
EntityManagerFactory emfdb = Persistence.createEntityManagerFactory("agisdb");
Yet, I got the following error message:
但是,我收到以下错误消息:
2009-07-21 09:22:41,018 [main] ERROR - No Persistence provider for EntityManager named agisdb javax.persistence.PersistenceException: No Persistence provider for EntityManager named agisdb at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
Here is the persistence.xml
:
这是persistence.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="agisdb">
<class>com.agis.livedb.domain.AddressEntity</class>
<class>com.agis.livedb.domain.TrafficCameraEntity</class>
<class>com.agis.livedb.domain.TrafficPhotoEntity</class>
<class>com.agis.livedb.domain.TrafficReportEntity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/agisdb"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
It should have been in the classpath. Yet, I got the above error.
它应该在类路径中。然而,我得到了上述错误。
采纳答案by Robert A Henru
After <persistence-unit name="agisdb">
, define the persistence provider name:
之后<persistence-unit name="agisdb">
,定义持久化提供者名称:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
回答by user225932
Make sure that the persistence.xml
file is in the directory: <webroot>/WEB-INF/classes/META-INF
确保persistence.xml
文件在目录中:<webroot>/WEB-INF/classes/META-INF
回答by Gilberto
Maybe you defined one provider like <provider>org.hibernate.ejb.HibernatePersistence</provider>
but referencing another one in jar. That happened with me: my persistence.xml provider was openjpa but I was using eclipselink in my classpath.
Hope this help!
也许你定义了一个提供者,<provider>org.hibernate.ejb.HibernatePersistence</provider>
但在 jar 中引用了另一个。这发生在我身上:我的 persistence.xml 提供程序是 openjpa,但我在我的类路径中使用了 eclipselink。希望这有帮助!
回答by Rob
Corner case: if you are using m2Eclipse, it automatically puts in excludes on your resources folders. Then when you try to run tests inside eclipse, the subsequent absence of persistence.xml will produce this error.
特殊情况:如果您使用的是 m2Eclipse,它会自动在您的资源文件夹中放入 excludes。然后当您尝试在 eclipse 中运行测试时,随后缺少 persistence.xml 将产生此错误。
回答by mahesh
It happenes when the entity manager is trying to point to many persistence units. Do the following steps:
当实体管理器试图指向许多持久性单元时会发生这种情况。执行以下步骤:
- open the related file in your editor (provided your project has been closed in your IDE)
- delete all the persistence and entity manager related code
- save the file
- open the project in your IDE
- now bind the db or table of your choice
- 在您的编辑器中打开相关文件(前提是您的项目已在您的 IDE 中关闭)
- 删除所有持久化和实体管理器相关的代码
- 保存文件
- 在 IDE 中打开项目
- 现在绑定您选择的数据库或表
回答by Ualter Jr.
Put the "hibernate-entitymanager.jar"
in the classpath of application.
For newer versions, you should use "hibernate-core.jar"
instead of the deprecated hibernate-entitymanager
将 放在"hibernate-entitymanager.jar"
应用程序的类路径中。
对于较新的版本,您应该使用"hibernate-core.jar"
而不是已弃用的hibernate-entitymanager
If you are running through some IDE, like Eclipse: Project Properties -> Java Build Path -> Libraries.
如果您正在运行某些 IDE,例如Eclipse:Project Properties -> Java Build Path -> Libraries.
Otherwise put it in the /lib
of your application.
否则把它放在/lib
你的应用程序中。
回答by Rónán ó Braonáin
The question has been answered already, but just wanted to post a tip that was holding me up. This exception was being thrown after previous errors. I was getting this:
这个问题已经得到了回答,但只是想发布一个让我感到困惑的提示。在之前的错误之后抛出此异常。我得到了这个:
property toplink.platform.class.name is deprecated, property toplink.target-database should be used instead.
Even though I had changed the persistence.xml to include the new property name:
即使我已经更改了 persistence.xml 以包含新的属性名称:
<property name="toplink.target-database" value="oracle.toplink.platform.database.oracle.Oracle10Platform"/>
Following the message about the deprecated property name I was getting the same PersistenceException like above and a whole other string of exceptions. My tip: make sure to check the beginning of the exception sausage.
在有关已弃用的属性名称的消息之后,我得到了与上面相同的 PersistenceException 和一整串其他异常。我的提示:确保检查异常香肠的开头。
There seems to be a bug in Glassfish v2.1.1 where redeploys or undeploys and deploys are not updating the persistence.xml, which is being cached somewhere. I had to restart the server and then it worked.
Glassfish v2.1.1 中似乎存在一个错误,即重新部署或取消部署和部署不会更新被缓存在某处的persistence.xml。我不得不重新启动服务器,然后它才能工作。
回答by col.panic
There is another point: If you face this problem within an Eclipse RCP environment, you might have to change the Factory generation from Persistence.createEntityManagerFactory
to new PersistenceProvider().createEntityManagerFactory
还有一点:如果您在 Eclipse RCP 环境中遇到这个问题,您可能需要将 Factory 代从 更改Persistence.createEntityManagerFactory
为new PersistenceProvider().createEntityManagerFactory
see ECFfor a detailed discussion on this.
有关这方面的详细讨论,请参阅ECF。
回答by harcharan
Make sure you have created persistence.xml file under the 'src' folder. I created under the project folder and that was my problem.
确保您已在“src”文件夹下创建了 persistence.xml 文件。我在项目文件夹下创建,这是我的问题。
回答by schieferstapel
In an OSGi-context, it's necessary to list your persistence units in the bundle's MANIFEST.MF, e.g.
在 OSGi 上下文中,有必要在包的 MANIFEST.MF 中列出您的持久性单元,例如
JPA-PersistenceUnits: my-persistence-unit
Otherwise, the JPA-bundle won't know your bundle contains persistence units.
否则,JPA 包不会知道您的包包含持久性单元。
See http://wiki.eclipse.org/EclipseLink/Examples/OSGi/Developing_with_EclipseLink_OSGi_in_PDE.
请参阅http://wiki.eclipse.org/EclipseLink/Examples/OSGi/Developing_with_EclipseLink_OSGi_in_PDE。