javax.persistence.PersistenceException:没有名为 example 的 EntityManager 的持久性提供程序
我正在使用 Hibernate 实体管理器。它抱怨它找不到持久性提供程序,但仍然将其记录在日志中:
TRACE 2011-02-21 22:17:52,843 [main](Ejb3Configuration.java:321) org.hibernate.ejb.Ejb3Configuration - PersistenceMetadata(version=2.0) [
name: sample
jtaDataSource: null
nonJtaDataSource: null
transactionType: RESOURCE_LOCAL
provider: org.hibernate.ejb.HibernatePeristence
useQuotedIdentifiers: false
classes[
]
packages[
]
mappingFiles[
]
jarFiles[
]
hbmfiles: 0
properties[
hibernate.connection.username: scott
hibernate.connection.password: tiger
hibernate.dialect: org.hibernate.dialect.OracleDialect
hibernate.show_sql: true
hibernate.connection.url: jdbc:oracle:thin:@localhost:1521:krsna
hibernate.archive.autodetection: class, hbm
hibernate.connection.driver_class: oracle.jdbc.driver.OracleDriver
]]
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named sample
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at EMPersistDemo2.main(EMPersistDemo2.java:26)
很明显,它找到了它,记录了它......但仍然抛出该异常。 我错过了什么吗?
我正在使用 Maven,并将 persistence.xml 放置在 src/main/resources/META-INF 中。 其内容是:
<!-- persistence.xml -->
<persistence ...>
<persistence-unit name="sample">
<provider>org.hibernate.ejb.HibernatePeristence</provider>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:krsna"/>
<property name="hibernate.connection.username" value="scott"/>
<property name="hibernate.connection.password" value="tiger"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
org.hibernate.Ejb3Configuration 能够找到它 javax.persitence.Persistence 无法找到它:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sample");
I am using Hibernate Entity Manager. It complains that it cant find persistence provider but still logs it in the logs :
TRACE 2011-02-21 22:17:52,843 [main](Ejb3Configuration.java:321) org.hibernate.ejb.Ejb3Configuration - PersistenceMetadata(version=2.0) [
name: sample
jtaDataSource: null
nonJtaDataSource: null
transactionType: RESOURCE_LOCAL
provider: org.hibernate.ejb.HibernatePeristence
useQuotedIdentifiers: false
classes[
]
packages[
]
mappingFiles[
]
jarFiles[
]
hbmfiles: 0
properties[
hibernate.connection.username: scott
hibernate.connection.password: tiger
hibernate.dialect: org.hibernate.dialect.OracleDialect
hibernate.show_sql: true
hibernate.connection.url: jdbc:oracle:thin:@localhost:1521:krsna
hibernate.archive.autodetection: class, hbm
hibernate.connection.driver_class: oracle.jdbc.driver.OracleDriver
]]
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named sample
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at EMPersistDemo2.main(EMPersistDemo2.java:26)
It is evident that it finds it, logs it ...but still throws that exception.
Am I missing some thing ?
I am using maven and I placed persistence.xml in src/main/resources/META-INF.
Its contents are :
<!-- persistence.xml -->
<persistence ...>
<persistence-unit name="sample">
<provider>org.hibernate.ejb.HibernatePeristence</provider>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:krsna"/>
<property name="hibernate.connection.username" value="scott"/>
<property name="hibernate.connection.password" value="tiger"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
org.hibernate.Ejb3Configuration is able to find it javax.persitence.Persistence is not able to find it:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sample");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到了同样的问题,但我得到了一个解决方案:缺少 META-INF/services/javax.persistence.spi.PersistenceProvider 文件。对于 hibernate,它位于 hibernate-entitymanager-XXX.jar 中,因此,如果 hibernate-entitymanager-XXX.jar 不在您的类路径中,您也会收到此错误。
I have also faced same issue but I got a solution that : missing META-INF/services/javax.persistence.spi.PersistenceProvider file. 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.
我在 java coderanch 上找到了我的问题的答案。
我的持久性提供程序在
persistence.xml
中的名称有一个拼写错误:我将
org.hibernate.ejb.HibernatePeristence
重命名为,它开始工作。
我发布我的答案,以便对其他人有所帮助。
I found the answer to my question on java coderanch.
There is a typo in my name of my persistence provider in
persistence.xml
:I renamed
org.hibernate.ejb.HibernatePeristence
toand it started working.
I am posting my answer so that this will be helpful to others.
闻起来好像 hibernate jar 文件不在类路径中,因为错误消息显示“未找到持久性提供程序”
smells like the hibernate jar files are not in the classpath since the error message says "persistence provider not found"