EclipseLink:没有为名为EntityManager的持久性提供程序。

7
我希望创建一个可以使用Java Persistence的Bundle。为了实现这一目标,我已在Eclipse中创建了一个插件项目。在我的项目中,我已经在META-INF中创建了一个persistence.xml文件。我还在我的MANIFEST.mf(依赖项中)添加了这3个包:
  1. javax.persistence.jar
  2. org.eclipse.persistence.jar
  3. org.eclipse.persistence.jar
然后,在我的激活器中,我使用以下代码行来创建一个实体管理器:
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 
EntityManager em = factory.createEntityManager();

为了执行我的捆绑包,我已经创建了一个产品配置。当我运行我的产品配置时,出现了以下错误:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named people 我尝试移动我的persistence.xml文件的位置,但没有成功。似乎没有任何包加载persistence.xml文件。也许我没有导入正确的包?
您可以在此处下载我的简单Bundle:download 您能否帮助我找到解决方案或线索?
5个回答

6
我已经解决了我的问题。我只需要在清单文件的类路径中添加以下包: - persistence.jar - eclipselink.jar - mysql-connector.jar
谢谢。

我不确定是否明白,你是否给那些JAR文件添加了清单?能否再详细说明一下? - simgineer

4
请在 persistence.xml 文件中添加以下标签:
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

1
谢谢,但是我在我的persistence.xml文件中已经有这行了。还有其他解决方案吗? - user376112

3

我在Eclipse中运行的一个简单项目中遇到了同样的错误。

事实证明,将包含persistence.xml的META-INF目录添加到我的类路径是错误的做法。

必须将其包含目录(或JAR文件)添加到类路径中。根据EclipseLink 2.5.1源代码:

   /**
     * Search the classpath for persistence archives. A persistence archive is
     * defined as any part of the class path that contains a META-INF directory
     * with a persistence.xml file in it. Return a list of {@link Archive}
     * representing the root of those files. It is the caller's responsibility
     * to close all the archives.
     * 
     * @param loader the class loader to get the class path from
     */
    public static Set<Archive> findPersistenceArchives(ClassLoader loader){
        // allow alternate persistence location to be specified via system property.  This will allow persistence units
        // with alternate persistence xml locations to be weaved
        String descriptorLocation = System.getProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML_DEFAULT);
        return findPersistenceArchives(loader, descriptorLocation);
    }

2

如果您正在使用Maven在eclipse中运行应用程序,请右键单击JPA项目,选择属性,并查看是否有“部署装配”菜单项。 如果有,则单击“部署装配”,单击“添加...”按钮,单击Java构建路径条目,然后选择Maven依赖项。请确保eclipselink.jar位于Maven依赖项之中。


2

1
我已经解决了我的问题。我只需要在清单文件的类路径中加入以下包:
  • persistence.jar
  • eclipselink.jar
  • mysql-connector.jar
谢谢。
- user376112

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接