This is not a duplicate as the tag above purports. This question has not yet been answered.
I am trying to set up this tutorial to get hyperjaxb
to work in an eclipse project. How can I get it to see a persistence provider? hbm2ddl
has NOT created the table structure in the database yet. Is that why the app is not seeing a persistence provider? Or is it because the contents of persistence.xml
in target/generated-sources
are not accessible from the src/main/java
directory in which my TestFunctions.java
is trying to call it? Here are the specifics... This line of code:
entityManagerFactory = Persistence.createEntityManagerFactory("persistence.xml", persistenceProperties);
Is throwing this error:
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)
The code throwing the error is in this method:
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
is:
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
The directory structure for the eclipse project is:
**EDIT: **
Just for kicks, I moved the META-INF
folder to the same level as the calling class, but I get the same error. Here are two new screen shots showing locations of persistence.xml in which the error is still thrown:
SECOND EDIT:
I added Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
at line 116 of TestFunctions.java
, which triggers the following error when I right click on Main.java
and do 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)
Here is the new TestFunctions.setUpPersistence()
method:
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);
}
When I replace Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
with Class c = Class.forName("org.hibernate.ejb.HibernatePersistence");
, I get a similar error. However, Class c = Class.forName("javax.persistence.spi.PersistenceProvider");
does not throw any error, so the program continues until the same javax.persistence.PersistenceException: No Persistence provider for EntityManager named org.jvnet.hyperjaxb3.ejb.tests.po
error gets thrown further downstream.
Does this tell us more about the cause of the error? Also, hyperjaxb
creates a persistence.xml
and places it in the target folder. This error occurred when persistence.xml
was in that location, and has persisted when I move persistence.xml
as shown above. Is hyperjaxb
causing this problem by not playing nicely with eclipse?