问题
I am getting the No Persistence provider for EntityManager for named X in an RCP Plugin application I am working on. I have used hibernate as ORM and javax.persistence. I am using Derby for my database. All the hibernate Jar and supporting jars i have grouped into a plugin and added to the current project i am woking on. I am at my wits end. Any help would be appreciated. i have as of now looked at different places for answers but not found any.
I have placed the Persistence.xml file in the classpath under/META-INF/persistence.xml. I have tried placing the persistence.xml at these location - /RCMSecond/src/persistence.xml and /RCMSecond/src/META-INF/persistence.xml and /RCMSecond/META-INF/persistence.xml still the RCP project is throwing the same error stacktrace.
persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="RCMSecond" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>entity.ColumnMeta</class>
<class>entity.DataBase_CompositeID</class>
<class>entity.Files</class>
<class>entity.Database</class>
<class>entity.LookUpCols</class>
<class>entity.Project</class>
<class>entity.QueryEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:simpleDB;create=true" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="show_sql" value="true" />
<property name="format_sql" value="true"></property>
</properties>
</persistence-unit>
</persistence>
GenericDAOImpl.java
package daoImpl;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.hibernate.Criteria;
import org.hibernate.Session;
import dao.GenericDAO;
import exceptions.EntityNotPresent;
public class GenericDAOImpl < T, ID extends Serializable > implements GenericDAO < T, ID > {
protected EntityManager entityManager;
protected EntityTransaction entityTransaction;
public GenericDAOImpl() {
super();
EntityManagerFactory factory = Persistence.createEntityManagerFactory("RCMSecond");
this.entityManager = factory.createEntityManager();
entityTransaction = this.entityManager.getTransaction();
}
@
Override
public T save(T t) {
entityTransaction.begin();
entityManager.persist(t);
entityManager.flush();
entityTransaction.commit();
return t;
}
@
SuppressWarnings({
"unchecked", "rawtypes"
})@ Override
public List < T > readAll(String namedQueryName, Class clazz) {
TypedQuery < T > query = entityManager.createNamedQuery(namedQueryName, clazz);
return query.getResultList();
}
@
SuppressWarnings("unchecked")@ Override
public T readById(@SuppressWarnings("rawtypes") Class clazz, ID id) {
entityTransaction.begin();
T t = (T) entityManager.find(clazz, id);
entityTransaction.commit();
return t;
}
@
Override
public T update(@SuppressWarnings("rawtypes") Class clazz, ID id, T updated) throws EntityNotPresent {
if (isEntityExists(clazz, id)) {
entityManager.merge(updated);
return updated;
} else {
throw new EntityNotPresent("Entity Not found, So Could not be updated");
}
}
@
Override
public void delete(@SuppressWarnings("rawtypes") Class clazz, ID removeId) {
if (isEntityExists(clazz, removeId)) {
T old = readById(clazz, removeId);
entityTransaction.begin();
entityManager.remove(old);
entityTransaction.commit();
}
}
@
SuppressWarnings("unchecked")@ Override
public boolean isEntityExists(@SuppressWarnings("rawtypes") Class clazz, ID id) {
return entityManager.find(clazz, id) != null;
}
@
SuppressWarnings("unchecked")@ Override
public T getFirstRecord(Class <? > clazz) {
entityTransaction.begin();
Session session = entityManager.unwrap(Session.class);
Criteria queryCriteria = session.createCriteria(clazz);
queryCriteria.setFirstResult(0);
queryCriteria.setMaxResults(1);
T t = (T) queryCriteria.list().get(0);
entityTransaction.commit();
return t;
}
@
Override
public List < T > getByQuery(String queryExecute, Object[] pars, @SuppressWarnings("rawtypes") Class clazz) {
entityTransaction.begin();@
SuppressWarnings("unchecked")
TypedQuery < T > query = entityManager.createQuery(queryExecute, clazz);
for (int i = 0; i < pars.length; i++) {
query.setParameter("arg" + i, pars[i]);
}
List < T > results = query.getResultList();
entityTransaction.commit();
return results;
}
}
StackTrace
org.eclipse.e4.core.di.InjectionException: javax.persistence.PersistenceException: No Persistence provider
for EntityManager named RCMSecond
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java: 68)
at org.eclipse.e4.core.internal.di.InjectorImpl.processAnnotated(InjectorImpl.java: 898)
at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java: 121)
at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java: 345)
at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java: 264)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java: 162)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java: 104)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java: 73)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java: 55)
at org.eclipse.e4.ui.workbench.renderers.swt.ContributedPartRenderer.createWidget(ContributedPartRenderer.java: 129)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createWidget(PartRenderingEngine.java: 971)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 640)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 746)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$0(PartRenderingEngine.java: 717)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$2.run(PartRenderingEngine.java: 711)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java: 42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java: 695)
at org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer.showTab(StackRenderer.java: 1306)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer$1.handleEvent(LazyStackRenderer.java: 72)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler$1.run(UIEventHandler.java: 40)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java: 186)
at org.eclipse.swt.widgets.Display.syncExec(Display.java: 4761)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application$1.syncExec(E4Application.java: 211)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler.handleEvent(UIEventHandler.java: 36)
at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java: 197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java: 197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java: 1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java: 230)
at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java: 148)
at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java: 135)
at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java: 78)
at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java: 39)
at org.eclipse.e4.ui.services.internal.events.EventBroker.send(EventBroker.java: 85)
at org.eclipse.e4.ui.internal.workbench.UIEventPublisher.notifyChanged(UIEventPublisher.java: 59)
at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java: 374)
at org.eclipse.e4.ui.model.application.ui.impl.ElementContainerImpl.setSelectedElement(ElementContainerImpl.java: 171)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.postProcess(LazyStackRenderer.java: 108)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 658)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 746)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$0(PartRenderingEngine.java: 717)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$2.run(PartRenderingEngine.java: 711)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java: 42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java: 695)
at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java: 71)
at org.eclipse.e4.ui.workbench.renderers.swt.SashRenderer.processContents(SashRenderer.java: 151)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 654)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 746)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$0(PartRenderingEngine.java: 717)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$2.run(PartRenderingEngine.java: 711)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java: 42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java: 695)
at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java: 71)
at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveRenderer.processContents(PerspectiveRenderer.java: 49)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 654)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 746)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$0(PartRenderingEngine.java: 717)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$2.run(PartRenderingEngine.java: 711)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java: 42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java: 695)
at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveStackRenderer.showTab(PerspectiveStackRenderer.java: 82)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer$1.handleEvent(LazyStackRenderer.java: 72)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler$1.run(UIEventHandler.java: 40)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java: 186)
at org.eclipse.swt.widgets.Display.syncExec(Display.java: 4761)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application$1.syncExec(E4Application.java: 211)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler.handleEvent(UIEventHandler.java: 36)
at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java: 197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java: 197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java: 1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java: 230)
at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java: 148)
at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java: 135)
at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java: 78)
at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java: 39)
at org.eclipse.e4.ui.services.internal.events.EventBroker.send(EventBroker.java: 85)
at org.eclipse.e4.ui.internal.workbench.UIEventPublisher.notifyChanged(UIEventPublisher.java: 59)
at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java: 374)
at org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl.setSelectedElement(PerspectiveStackImpl.java: 135)
at org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl.setSelectedElement(PerspectiveStackImpl.java: 1)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.postProcess(LazyStackRenderer.java: 108)
at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveStackRenderer.postProcess(PerspectiveStackRenderer.java: 63)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 658)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 746)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$0(PartRenderingEngine.java: 717)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$2.run(PartRenderingEngine.java: 711)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java: 42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java: 695)
at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java: 71)
at org.eclipse.e4.ui.workbench.renderers.swt.WBWRenderer.processContents(WBWRenderer.java: 614)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 654)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java: 746)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$0(PartRenderingEngine.java: 717)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$2.run(PartRenderingEngine.java: 711)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java: 42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java: 695)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java: 1057)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java: 337)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java: 1018)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java: 156)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java: 159)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java: 196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java: 134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java: 104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java: 380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java: 235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 669)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java: 608)
at org.eclipse.equinox.launcher.Main.run(Main.java: 1515)
at org.eclipse.equinox.launcher.Main.main(Main.java: 1488)
Caused by: javax.persistence.PersistenceException: No Persistence provider
for EntityManager named RCMSecond
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java: 61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java: 39)
at daoImpl.GenericDAOImpl. < init > (GenericDAOImpl.java: 30)
at daoImpl.DatabaseDAOImpl. < init > (DatabaseDAOImpl.java: 14)
at com.view.FirstView.createControls(FirstView.java: 31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java: 56)
...112 more
I have tried placing the persistence.xml at these location - /RCMSecond/src/persistence.xml and /RCMSecond/src/META-INF/persistence.xml still the RCP project is throwing the same error stacktrace. Any help would be appreciated. Thanks in Advance
回答1:
https://docs.oracle.com/javaee/7/tutorial/persistence-intro003.htm
The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit's root.
Persistent units can be packaged as part of a WAR or EJB JAR file or can be packaged as a JAR file that can then be included in an WAR or EAR file.
• If you package the persistent unit as a set of classes in an EJB JAR file, persistence.xml should be put in the EJB JAR's META-INF directory. • If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file's WEB-INF/classes/META-INF directory. • If you package the persistence unit in a JAR file that will be included in a WAR or EAR file, the JAR file should be located in either - the WEB-INF/lib directory of a WAR Or - the EAR file's library directory
Check configuration. Your Eclipse should be able to map resource files and build all packages. check Project->Properties->Build Path in order to configure project dependencies.
offtopic tips: You are trying to obtain the EntityManager by injection:
@PersistenceContext
protected EntityManager entityManager
Then to obtain the EntityManager from the EntityManagerFactory instance:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("RCMSecond");
this.entityManager = factory.createEntityManager();
来源:https://stackoverflow.com/questions/37481321/no-persistence-provider-for-entitymanager-in-rcp-pulgin-project