javax.persistence.PersistenceException:没有名为customerManager的EntityManager的持久性提供者 [英] javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager
问题描述
我是JPA&休眠。在阅读了一些在线资料后,我现在了解了Hibernate是什么以及它如何与JPA一起使用。
现在,我试图运行这个 JPA& Hibernate教程。我已经完成了本教程中提到的所有内容。
我没有Oracle DB,只有MySQL。所以我使用我对JPA&我的理解对 persistence.xml
做了一些修改。 Hibernate(我不知道它是否正确......对我来说就是这样。)
这是我的 persistence.xml
< persistence xmlns =http://java.sun.com/xml/ns/持久性xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemalocation =http://java.sun.com/xml/ns/persistence http://java.sun .com / xml / ns / persistence / persistence_1_0.xsdversion =1.0>
< persistence-unit name =customerManagertransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class>客户< / class>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLInnoDBDialect/>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.connection.usernamevalue =root/>
< property name =hibernate.connection.passwordvalue =1234/>
< property name =hibernate.max_fetch_depthvalue =3/>
< / properties>
< / persistence-unit>
< /余辉>
但我似乎没有得到他们描述的输出。它给了我:
创建之前的客户id:null
log4j:WARN无法为记录器找到appender(组织.hibernate.cfg.annotations.Version)。
log4j:WARN请正确初始化log4j系统。
线程main中的异常javax.persistence.PersistenceException:否EntityManager的持久性提供程序,名称为customerManager $ b $,位于javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
,位于javax.persistence CustomerDAO.create(CustomerDAO.java:8)
在CustomerDAO.main(CustomerDAO.java:22)
$ c $ .Bersistence.createEntityManagerFactory(Persistence.java:33)
at CustomerDAO.create c>
欢迎您提出任何建议。
更新:
我已完成要求完成的更改。但是,仍然得到asme错误行!!!
在本教程中,他们没有提及任何关于 orm.xml 的内容。可能它是一个问题!
您的 persistence.xml
无效,并且 EntityManagerFactory
无法创建。它应该是:
< persistence xmlns =http://java.sun.com/xml/ns/persistence xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemalocation =http://java.sun.com/xml/ns/persistence http://java.sun.com /xml/ns/persistence/persistence_1_0.xsdversion =1.0>
< persistence-unit name =customerManagertransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class>客户< / class>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLInnoDBDialect/>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.connection.usernamevalue =root/>
< property name =hibernate.connection.passwordvalue =1234/>
< property name =hibernate.max_fetch_depthvalue =3/>
< / properties>
< / persistence-unit>
< /余辉>
(注意< property>
元素是关闭的,它们不应该嵌套)
更新:我阅读了教程,您还必须更改 Id
使用MySQL时的生成策略(因为MySQL不支持序列)。我建议使用 AUTO
策略(默认为使用MySQL的IDENTITY)。为此,删除 SequenceGenerator
注释并更改代码,如下所示:
@Entity
@Table(name =TAB_CUSTOMER)
public class Customer实现Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =CUSTOMER_ID,precision = 0)
private long customerId = null;
...
}
这应该有所帮助。 / p>
PS:您还应该按照建议提供 log4j.properties
。
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.
Now, I am trying to run this JPA & Hibernate tutorial. I've done everything they mention in this tutorial.
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.)
Here is my 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!!!
They didnt mentioned anything about orm.xml in that tutorial. may it be a problem causer!!!
Your persistence.xml
is not valid and the EntityManagerFactory
can't get created. It should be:
<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)
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:
@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.
这篇关于javax.persistence.PersistenceException:没有名为customerManager的EntityManager的持久性提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!