我遇到No Persistence provider for EntityManager
例外,无法弄清楚是什么导致了它。这是我的配置文件:
Persistence.xml (存储在src/META-INF
)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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_2_0.xsd">
<persistence-unit name="HatifimJPA" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entities.HatUser</class>
<properties>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.username" value="Benny" />
<property name="hibernate.connection.password" value="oracle" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
</properties>
</persistence-unit>
</persistence>
例外:
线程“main”中的异常javax.persistence.PersistenceException:没有名为HatifimJPA的EntityManager的持久性提供程序 在javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) 在javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) 在testing.TestClass.main(TestClass.java:16)
唯一的区别是我试图让EntityManager
进入public void main(...) { ... }
区块,但是,我不记得过去这样做有问题。
任何人都可以帮忙指出问题所在吗?
答案 0 :(得分:3)
尝试将persistence.xml放到src / main / resources / META-INF / 并将此代码放入main方法:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HatifimJPA");
EntityManager em = emf.createEntityManager();