JPA createEntityManagerFactory “Unknown Source” error (non-J2EE)

限于喜欢 提交于 2019-12-23 04:25:06

问题


I can't find what's wrong: many people seem to have issues with creating a EntityManagerFactory using the PU name. But I've checked all the suggestions, and none helped so far. I'm trying to build my first JavaFX2 app. It is a small dictionary application, originally built on the Netbeans Application framework (by extending the default "Master-Detail" generated classes). It uses a SQLite db.

I'm now porting it over from there to another (JavaFX2-enabled) project. I first set out to rebuild the JPA persistence stuff in the new app. As far as I can see, I have everything in place: META-INF/persistence.xml with all the necessary entity classes declarated, using the same DB connection as the original application.

I added eclipselink-2.3.0.jar; eclipselink-javax.persistence-2.0.jar and eclipselink-jpa-modelgen-2.3.0.jar to the project.

Instead of the Application Context I use a simple Properties class to hold the data (such as queries) that previously went in the .properties files.

This is the test class:

    package ithildinfx;

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.Group;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.stage.Stage;
    import javafx.beans.property.StringProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;

    public class IthildinFX extends Application {
       private void init(Stage primaryStage) {
          entityManager = javax.persistence.Persistence.createEntityManagerFactory("IthildinFXPU").createEntityManager(); 
          entryQuery = entityManager.createQuery(Properties.entryQuery).setParameter("gloss", "a%"); 
          entryList = entryQuery.getResultList();

          Group root = new Group();
          primaryStage.setScene(new Scene(root));
          (TableColumn definitions)
          (TableView code)
          root.getChildren().add(tableView);
       } 

       @Override 
       public void start(Stage primaryStage) throws Exception {
          init(primaryStage);
          primaryStage.show();
       }

       public static void main(String[] args) { 
          launch(args); 
       }

       private javax.persistence.EntityManager entityManager;
       private java.util.List<ithildinfx.Entry> entryList;
       private javax.persistence.Query entryQuery;
   }

And this is the contents of persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
      <persistence-unit name="IthildinFXPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>ithildinfx.Dataclass</class>
        <class>ithildinfx.Metadata</class>
        <class>ithildinfx.Translation</class>
        <class>ithildinfx.Entry</class>
        <class>ithildinfx.TrMd</class>
        <class>ithildinfx.Language</class>
        <properties>
          <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:lib/db/ithildin-13-11-11.db"/>
          <property name="javax.persistence.jdbc.password" value=""/>
          <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC"/>
          <property name="javax.persistence.jdbc.user" value=""/>
        </properties>
      </persistence-unit>
    </persistence>

One of the suggestions that I read here on StackOverflow was to add this line to manifest.mf:

    Meta-Persistence: META-INF/persistence.xml

and, possibly:

    JPA-PersistenceUnits: IthildinFXPU

But neither made any difference.

This is the output that I invariably get:

Building jar: /Users/luthien/NetBeansProjects/IthildinFX/dist/IthildinFX.jar
Copy libraries to /Users/luthien/NetBeansProjects/IthildinFX/dist/lib.
Building jar: /Users/luthien/NetBeansProjects/IthildinFX/dist/IthildinFX.jar
Detected JavaFX Ant API version 1.1
Deleting: /Users/luthien/NetBeansProjects/IthildinFX/dist/IthildinFX.jar
Deleting: /Users/luthien/NetBeansProjects/IthildinFX/dist/lib/IthildinFX.jar
Deleting: /Users/luthien/NetBeansProjects/IthildinFX/dist/README.TXT
Skip jar copy to itself: IthildinFX.jar
Skip jar copy to itself: lib/eclipselink-2.3.0.jar
Skip jar copy to itself: lib/eclipselink-javax.persistence-2.0.jar
Skip jar copy to itself: lib/eclipselink-jpa-modelgen-2.3.0.jar
Skip jar copy to itself: lib/sqlite-jdbc-3.7.2.jar
jfx-deployment:
jar:
run:
Exception in Application start method
java.lang.NullPointerException
    at javax.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver.getProviderNames(Unknown Source)
    at javax.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver.getPersistenceProviders(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at ithildinfx.IthildinFX.init(IthildinFX.java:29)
    at ithildinfx.IthildinFX.start(IthildinFX.java:109)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:298)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:136)
    at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:108)
JavaFX application launcher: calling System.exit
jfxsa-run:
BUILD SUCCESSFUL (total time: 5 seconds)

The libraries seem to all be in the right place - in any case, they are in the same place as they are in the first application. It might be that the Netbeans Application framework does some scary voodoo under the bonnet, but I really don't know how to figure that out.

Does anyone know of a reason why it can't seem to find the PU?

Thanks in advance! Luthien


回答1:


There error is coming from the JPA library itself, it seems that JPA is not configured correctly. The issue dos not seem to have anything to do with the persistence unit, the JPA library seems to have issues, looks like it can't find its services file resource.

Maybe remove eclipselink-javax.persistence-2.0.jar, if you server already has JPA on its classpath, there could be a conflict.



来源:https://stackoverflow.com/questions/9467015/jpa-createentitymanagerfactory-unknown-source-error-non-j2ee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!