RCP Pulgin Project中的EntityManager没有持久性提供程序

时间:2016-05-27 10:39:44

标签: jpa eclipse-plugin eclipse-rcp

我正在为我正在处理的RCP插件应用程序中的命名X获取EntityManager的No Persistence提供程序。我已经将hibernate用作ORM和javax.persistence。我正在使用Derby作为我的数据库。所有的hibernate Jar和支持jar我已经分组成一个插件并添加到我正在进行的当前项目中。我没办法。任何帮助,将不胜感激。我现在看到不同的地方寻找答案,但没有找到任何答案。

我已将Persistence.xml文件放在/ META-INF / persistence.xml下的类路径中。我尝试将persistence.xml放在这些位置 - /RCMSecond/src/persistence.xml /RCMSecond/src/META-INF/persistence.xml /RCMSecond/META-INF/persistence.xml 仍然是RCP项目抛出相同的错误堆栈跟踪。

的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>
&#13;
&#13;
&#13;

GenericDAOImpl.java

&#13;
&#13;
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;

  }
}
&#13;
&#13;
&#13;

栈跟踪

&#13;
&#13;
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
&#13;
&#13;
&#13;

我尝试将persistence.xml放在这些位置 - /RCMSecond/src/persistence.xml /RCMSecond/src/META-INF/persistence.xml RCP项目仍在抛出相同的错误堆栈跟踪。 任何帮助,将不胜感激。在此先感谢

1 个答案:

答案 0 :(得分:0)

https://docs.oracle.com/javaee/7/tutorial/persistence-intro003.htm

  

META-INF目录包含的JAR文件或目录   persistence.xml被称为持久性单元的根。范围   持久性单元的长​​度由持久性单元的根确定。

     

持久单元可以打包为WAR或EJB JAR文件的一部分或   可以打包为JAR文件,然后可以包含在WAR或   EAR文件。

 • 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

检查配置。您的Eclipse应该能够映射资源文件并构建所有包。检查Project-&gt; Properties-&gt; Build Path以配置项目依赖项。

offtopic tips: 您正尝试通过注入获取EntityManager:

  @PersistenceContext
  protected EntityManager entityManager

然后从EntityManagerFactory实例获取EntityManager:

   EntityManagerFactory factory = Persistence.createEntityManagerFactory("RCMSecond");
   this.entityManager = factory.createEntityManager();