我在 META-INF 目录下使用 TopLink 拥有同名的 persistence.xml。然后,我有我的代码调用它:
private static EntityManagerFactory entityManagerFactory;
private static final String PERSISTENCE = "PERSISTENCE";
public static EntityManagerFactory getEntityManagerFactory() {
if (entityManagerFactory == null) {
entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE);
}
return entityManagerFactory;
}
但是,我收到以下错误消息:
Exception in thread "main" java.lang.ExceptionInInitializerError
at MainApp.initAccountCategory(MainApp.java:84)
at MainApp.main(MainApp.java:25)
原因:javax.persistence.PersistenceException:在 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85) 在 util.HibernateUtil 的 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) 中没有名为 PERSISTENCE 的 EntityManager 的持久性提供程序.getEntityManagerFactory(HibernateUtil.java:16) 在 common.AppContext.(AppContext.java:34)
这是persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_2.xsd
http://xmlns.jcp.org/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
version="2.2">
<persistence-unit name="PERSISTENCE">
<description>bank simulation application</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/bankmodul"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="Mahdi@123"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>