【问题标题】:Instanciate a JPA EntityManager in an Eclipse RCP (Java SE) environment在 Eclipse RCP (Java SE) 环境中实例化 JPA EntityManager
【发布时间】:2016-12-04 10:41:50
【问题描述】:

上下文:

  • Eclipse RCP 项目,分为插件,每个插件专门用于一项任务(例如:HMI 管理、通信等)

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

- 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 中定义的持久性单元名称

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

当我将此 main 作为 java 应用程序运行时,一切正常。

(为了防止将来出现问题:我的 persistence.xml 文件最初是在 MANIFEST.MF META-INF 文件夹中生成的,但是在运行这个 main 时,找不到它,所以我移动了它。我都检查了从另一个插件调用持久化函数时的配置)


问题:

我需要从另一个插件访问我的持久化功能,我们称他为 com.plugin.other

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

javax.persistence.PersistenceException:没有名为 com.plugin.name 的 EntityManager 的持久性提供程序
在 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
在 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)

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

【问题讨论】:

    标签: jpa eclipse-rcp persistence entitymanager jpa-2.1


    【解决方案1】:

    我终于在没有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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多