在 Eclipse RCP (Java SE) 环境中实例化 JPA EntityManager

Instanciate a JPA EntityManager in an Eclipse RCP (Java SE) environment

上下文:

需要在数据库中保存一些数据

- ORM : hibernate's JPA (JPA 2.1, Hibernate 5.2.1 used throught an org.hibernate.core plugin as constructed  [here (page 8)][1]
- database : in mysql 
- constraint : Java SE environment, so the only way to obtain an EntityManager (for the persistence operations) is to create it through an EntityManagerFactory 

第一步(有效!):

管理数据库操作的插件,其结构:

com.plugin.name
    JRE System Library
    Plug-in Dependencies 
    Referenced Libraries          (contains the mysql connector jar)
    src/
        com.plugin.name           (package containing the plugin activator)
        com.plugin.name.entities  (package containing all my entities)
        com.plugin.name.utils     (package containing my access functions and a main)
        META-INF/
            persistence.xml       
    META-INF/
        MANIFEST.MF

com.plugin.name.utils 中,我 类 执行所有持久性功能。

在这些 类 中,我以这种方式创建了一个 EntityManagerFactory :

private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory("com.plugin.name");

其中“com.plugin.name”是我的persistence.xml

中定义的持久化单元名称

在其中一个 类 中,我有一个主要的 运行ning 一些与数据库相关的函数。

当我运行这个主要作为java应用程序时,一切正常。

(为了防止以后出现问题:我的 persistence.xml 文件最初是在 MANIFEST.MF META-INF 文件夹中生成的,但是当 运行 设置这个主文件时,它不能找不到,所以我移动了它。我在从另一个插件调用持久性功能时检查了两个配置)


问题:

我需要从另一个插件访问我的持久性函数,我们称他为com.plugin.other

所以我添加了com.plugin.name作为这个插件的依赖。 但是当我尝试 运行 应用程序时,出现以下错误:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named com.plugin.name
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)

当我 运行 作为独立应用程序的主要应用程序时,如果 persistence.xml 文件有任何问题(未找到、不完整等),它至少会被提及...我在这里真的不知道问题出在哪里。

我终于在没有 persistence.xml 的情况下通过使用 PersistenceProvider 中的 createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) 方法做到了。

代码:

Activator a = Activator.getDefault();
Bundle b = a.getBundle();
URL url = b.getResource("META-INF/persistence.xml");

List<PersistenceProvider> providers = PersistenceProviderResolverHolder.getPersistenceProviderResolver().getPersistenceProviders();

for (PersistenceProvider pp : providers) {

    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl();
    pui.setPersistenceUnitName("persistenceUnitName");
    pui.setTransactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL);
    pui.setPersistenceUnitRootUrl(url);

    Properties prop = pui.getProperties();
    if (prop == null) {
        prop = new Properties();
    }
    prop.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://ip:port/dbName");
    prop.setProperty("javax.persistence.jdbc.user", "dbUser");
    prop.setProperty("javax.persistence.jdbc.password", "dbPass");
    prop.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");

    pui.setProperties(prop);

    pui.setClassLoader(com.mysql.jdbc.Driver.class.getClassLoader());     
        emFactory = pp.createContainerEntityManagerFactory(pui, null);
    }
}