带有eclipselink和derby数据库的Eclipse插件项目

时间:2012-04-17 18:14:02

标签: eclipse eclipselink derby rcp

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

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

但我不能用插件项目做到这一点。我将derby.jar,eclipselink.jar和java.persistence_2.0.3.jar添加到构建路径中。我还在dependecies选项卡中的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.

1 个答案:

答案 0 :(得分:1)

您必须在provider

中添加persistence.xml
...
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
...

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

实施例

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