正如上面的标签所声称的那样,这不是重复的。这个问题还没有被解答。
我正在尝试设置本教程以开始hyperjaxb
在 Eclipse 项目中工作。我怎样才能让它看到持久性提供者? hbm2ddl
尚未在数据库中创建表结构。这就是应用程序没有看到持久性提供程序的原因吗?还是因为无法从my试图调用它的目录访问persistence.xml
in的内容?以下是具体细节......这行代码: target/generated-sources
src/main/java
TestFunctions.java
entityManagerFactory = Persistence.createEntityManagerFactory("persistence.xml", persistenceProperties);
抛出此错误:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistence.xml
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at maintest.TestFunctions.setUpPersistence(TestFunctions.java:119)
at maintest.Main.main(Main.java:10)
抛出错误的代码在这个方法中:
public void setUpPersistence(){
final Properties persistenceProperties = new Properties();
InputStream is = null;
try {
Class<? extends TestFunctions> c = getClass();
ClassLoader cl = c.getClassLoader();
is = cl.getResourceAsStream("persistence.properties");
persistenceProperties.load(is);
}catch (IOException i) {i.printStackTrace();}
finally {if (is != null) {try {is.close();} catch (IOException ignored) {}}}
entityManagerFactory = Persistence.createEntityManagerFactory("persistence.xml", persistenceProperties);
}
persistence.xml
:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:orm="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<persistence-unit name="org.jvnet.hyperjaxb3.ejb.tests.po">
<class>org.jvnet.hyperjaxb3.ejb.tests.po.Items</class>
<class>org.jvnet.hyperjaxb3.ejb.tests.po.Items$Item</class>
<class>org.jvnet.hyperjaxb3.ejb.tests.po.PurchaseOrderType</class>
<class>org.jvnet.hyperjaxb3.ejb.tests.po.USAddress</class>
</persistence-unit>
</persistence>
persistence.properties
是:
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=someusername
hibernate.connection.password=somepassword
hibernate.connection.url=jdbc:mysql://localhost/somedatabasename
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
hibernate.jdbc.batch_size=0
eclipse项目的目录结构为:
**编辑: **
只是为了好玩,我将META-INF
文件夹移动到与调用类相同的级别,但我得到了同样的错误。这是两个新的屏幕截图,显示了仍然抛出错误的 persistence.xml 位置:
第二次编辑:
我Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
在第 116 行添加,TestFunctions.java
当我右键单击Main.java
并执行时触发以下错误Run As..Java Application
:
Exception in thread "main" java.lang.ClassNotFoundException: org.eclipse.persistence.jpa.PersistenceProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at maintest.TestFunctions.setUpPersistence(TestFunctions.java:116)
at maintest.Main.main(Main.java:10)
这是新TestFunctions.setUpPersistence()
方法:
public void setUpPersistence() throws ClassNotFoundException{
final Properties persistenceProperties = new Properties();
InputStream is = null;
try {
Class<? extends TestFunctions> c = getClass();
ClassLoader cl = c.getClassLoader();
is = cl.getResourceAsStream("persistence.properties");
persistenceProperties.load(is);
}catch (IOException i) {i.printStackTrace();}
finally {if (is != null) {try {is.close();} catch (IOException ignored) {}}}//org.jvnet.hyperjaxb3.ejb.tests.po
Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
entityManagerFactory = Persistence.createEntityManagerFactory(/*"org.jvnet.hyperjaxb3.ejb.tests.po"*//*"persistence.xml"*/"org.jvnet.hyperjaxb3.ejb.tests.po", persistenceProperties);
}
当我替换Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
为 时Class c = Class.forName("org.hibernate.ejb.HibernatePersistence");
,我收到类似的错误。但是,Class c = Class.forName("javax.persistence.spi.PersistenceProvider");
不会引发任何错误,因此程序javax.persistence.PersistenceException: No Persistence provider for EntityManager named org.jvnet.hyperjaxb3.ejb.tests.po
会继续运行,直到下游进一步引发相同的错误。
这是否告诉我们更多有关错误原因的信息? 此外,hyperjaxb
创建一个persistence.xml
并将其放置在目标文件夹中。此错误发生persistence.xml
在该位置时,并且当我persistence.xml
如上所示移动时仍然存在。是hyperjaxb
因为不能很好地使用 eclipse 导致这个问题吗?