PersistenceProvider createEntityManagerFactory在Eclipse插件中返回null

时间:2013-06-26 15:28:04

标签: java jpa eclipse-plugin osgi eclipselink

我创建了一个独立的应用程序来访问我的数据库(mssql或h2) 我创建了一个辅助类,通过传递连接细节连接到我的服务器。

一切都很好,按照预期的那样工作。

现在我将我的代码移动到Eclipse插件( - > OSGi) 首先我遇到了一个Driver Not Found错误,但我通过将ClassLoader传递给PersistenceProvider来解决这个问题。

现在我遇到了下一个问题,我无法解决: 当我调用 createEntityManagerFactory 传递持久性单元名称和我的地图时,PersistenceProvider返回 null

它不会抛出任何异常......只需返回 null

所以我猜驱动程序被发现了...... 由于独立应用程序正在运行,它不能是persistence.xml ...

任何想法?

这里是我的persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="EclipseLinkTest" transaction-type="RESOURCE_LOCAL">
  <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!--   <mapping-file>META-INF/contact-orm.xml</mapping-file>     -->
    <properties>
<!--       <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> -->
<!--       <property name="javax.persistence.jdbc.url" value="jdbc:h2:D:/JAVA/Final/lumo.osgi.service.eclipselink.impl/db/embedded.db" /> -->
<!--       <property name="javax.persistence.jdbc.user" value="admin" /> -->
<!--       <property name="javax.persistence.jdbc.password" value="admin" /> -->

<!--       <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> -->
<!--       <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://dbserverIP:1433;DatabaseName=testsystem" /> -->
<!--       <property name="javax.persistence.jdbc.user" value="editor" /> -->
<!--       <property name="javax.persistence.jdbc.password" value="editorpwd" /> -->


      <!-- EclipseLink should create the database schema automatically -->
<!--       <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> -->
<!--       <property name="eclipselink.ddl-generation" value="create-tables" /> -->
<!--       <property name="eclipselink.ddl-generation.output-mode" value="database" /> -->
<!--       <property name="eclipselink.weaving" value="true"/> -->
      <property name="eclipselink.logging.level" value="WARNING"/>
    </properties>

  </persistence-unit>
</persistence> 

这是获取配置的代码(应用程序是主类 - 因此其类加载器获取有关目标定义中所有内容的信息)

public Properties getEmbeddedConfiguration(String path, String database,
            String login, String password) {
        Properties props = new Properties();
        // this one is for getting started in OSGi & JPA

        // No suitable driver found for jdbc:h2:file:C:\temp\Thumbs.db;IFEXISTS=TRUE
//      props.put(PersistenceUnitProperties.CLASSLOADER,
//              EclipseLinkServiceImpl.class.getClassLoader());
        // this fixes the driver not found but leaves a nullpointer
        props.put(PersistenceUnitProperties.CLASSLOADER,
                Application.class.getClassLoader());

         String connectionString = String.format(
         "jdbc:h2:file:%s%s;IFEXISTS=TRUE", path, database);
         props.put("javax.persistence.jdbc.driver", "org.h2.Driver");
         props.put("javax.persistence.jdbc.url", connectionString);
         props.put("javax.persistence.jdbc.user", login);
         props.put("javax.persistence.jdbc.password", password);

         props.put("eclipselink.target-database",
         "org.eclipse.persistence.platform.database.H2Platform");
         props.put("eclipselink.ddl-generation", "drop-and-create-tables");
         props.put("eclipselink.ddl-generation.output-mode", "database");
        return props;
    }

最后但并非最不重要的是创建部分

PersistenceProvider p = new PersistenceProvider();
factory = p.createEntityManagerFactory(PERSISTENCE_UNIT_NAME,
getEmbeddedConfiguration(folder, database, username, pwd));
System.out.println("factory: " + factory);
em = factory.createEntityManager();

1 个答案:

答案 0 :(得分:0)

要在OSGi中运行JPA,请查看Gemini project