0

我设法使用 eclipselink 创建了一个 java 应用程序,如下所示

http://www.vogella.com/articles/JavaPersistenceAPI/article.html

但我不能用插件项目做到这一点。我在构建路径中添加了 derby.jar、eclipselink.jar 和 java.persistence_2.0.3.jar。我还在依赖项选项卡的 MANIFEST.MF 的依赖项中添加了 javax.persistence 和 org.apache.derby.jdbc 作为导入包

我的 persistence.xml 与 MANIFEST.MF 位于 META-INF 文件夹中

我的 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="todos" transaction-type="RESOURCE_LOCAL">
<class>Tutorial.Todo</class>
<properties>
    <property name="javax.persistence.jdbc.driver"value="org.apache.derby.jdbc.EmbeddedDriver" />
    <property name="javax.persistence.jdbc.url" value="jdbc:derby:simpleDb;create=true" />
    <property name="javax.persistence.jdbc.user" value="test" />
   <property name="javax.persistence.jdbc.password" value="test" />


   <property name="eclipselink.ddl-generation" value="create-tables" />
   <property name="eclipselink.ddl-generation.output-mode" value="database" />
     </properties>

</persistence-unit>

我这样称呼它:

private static final String PERSISTENCE_UNIT_NAME = "todos";
private static EntityManagerFactory factory;

public static void test {
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();
    // Read the existing entries and write to console
    Query q = em.createQuery("select t from Todo t");
    List<Todo> todoList = q.getResultList();
    for (Todo todo : todoList) {
        System.out.println(todo);
    }
    System.out.println("Size: " + todoList.size());

    // Create new todo
    em.getTransaction().begin();
    Todo todo = new Todo();
    todo.setSummary("This is a test");
    todo.setDescription("This is a test");
    em.persist(todo);
    em.getTransaction().commit();

    em.close();
}

我得到的错误信息是:

!ENTRY org.eclipse.osgi 4 0 2012-04-17 20:54:18.568
!MESSAGE Application error
!STACK 1
javax.persistence.PersistenceException: No Persistence provider for EntityManager named todos
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at rcptest.Application.start(Application.java:30)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
An error has occurred. See the log file
C:\dev\runtime-RcpTest.application\.metadata\.log.
4

1 回答 1

1

您必须将 a 添加provider到您的persistence.xml:

...
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
...

org.eclipse.persistence.jpa.PersistenceProviderEclipseLink的持久性提供程序。)

例子

<?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">

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
        <class>Tutorial.Todo</class>

        <properties>
            <!-- your properties -->
        </properties>
    </persistence-unit>

</persistence>
于 2012-04-29T09:32:56.193 回答