无法创建JavaFX中一个的EntityManager(Can't create a Ent

2019-09-02 16:29发布

我遇到了JPAJavaFX一个非常令人不愉快的和不明确的情况下,我试着问这个在其他两个线程分别是: JPA的EntityManager和JavaFX和实体管理器在JavaFX中不工作

但我不得不在这里开设一家新的线程:情况:

我使用NetBeansPostgres, 了JavaFx FXML应用 ,首先什么我做的是我添加了一个持久性单元 ,其给出了这样的:

与给定工作的意见和阅读此页后http://tomee.apache.org/jpa-concepts.html

  • 我发现,在JavaFX事务类型必须是“RESOURCE_LOCAL”,让我证实。
  • 我还发现,与模式的EntityManagerFactory必须通过@PersistenceUnit注解只(不@PersistenceContext)被注入,所以我也核实。

这里是我的代码看起来像

package rawda;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.persistence.Persistence;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author Aymen Daoudi
 */
public class Rawda extends Application
{
    @PersistenceUnit(unitName="RawdaPU")
    static private EntityManagerFactory emf;
    static 
    {
        try 
        {
            emf = Persistence.createEntityManagerFactory("RawdaPU");
            EntityManager em = emf.createEntityManager();
        } 
        catch (Exception e) 
        {
            System.out.println("Fatal: Unable to create entity manager factory");
            e.printStackTrace();
        }  
  }

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("View/MainView.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }
/**
 * The main() method is ignored in correctly deployed JavaFX application.
 * main() serves only as fallback in case the application can not be
 * launched through deployment artifacts, e.g., in IDEs with limited FX
 * support. NetBeans ignores main().
 *
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    launch(args);
}

现在,运行此代码,在该行产生了以下异常

EntityManager em = emf.createEntityManager();

这里是例外:

Exception Description: Configuration error.  Class [org.postgresql.Driver] not found.
Fatal: Unable to create entity manager factory
javax.persistence.PersistenceException: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error.  Class [org.postgresql.Driver] not found.
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:517)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:294)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:272)
    at rawda.Rawda.<clinit>(Rawda.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:276)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:662)
Caused by: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error.  Class [org.postgresql.Driver] not found.
    at org.eclipse.persistence.exceptions.DatabaseException.configurationErrorClassNotFound(DatabaseException.java:82)
    at org.eclipse.persistence.sessions.DefaultConnector.loadDriverClass(DefaultConnector.java:267)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:85)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:584)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:488)
    ... 13 more

所以,我不是能够创建EntityManager的与我的数据库进行交互,请澄清我,我错过了什么,在此先感谢。

Answer 1:

似乎从你不必在classpath Postgres的驱动程序堆栈跟踪非常清楚:

Exception Description: Configuration error.  Class [org.postgresql.Driver] not found.

司机是从下载postgres.org



文章来源: Can't create a EntityManager in JavaFx