0

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

一切都很好,并且按预期工作。

现在我将我的代码移动到一个 Eclipse 插件(-> OSGi)首先我遇到了一个驱动程序未找到错误,但我通过将我的 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();
4

1 回答 1

0

要让 JPA 在 OSGi 中运行,请查看Gemini 项目

于 2013-06-26T23:40:46.660 回答