AymenDaoudi
AymenDaoudi

Reputation: 8309

Can't create a EntityManager in JavaFx

I'm experiencing a very unpleasing and not clear case with JPA and JavaFx, I tried to ask about this in two other threads which are : JPA EntityManager and JavaFx and Entity Manager not working in JavaFX

but I was obliged to open a new thread here : the situation :

I am using NetBeans and Postgres, JavaFx Fxml Application , what first I do is that I add a persistence unit which gives this :

Project overview

after working with the given advice and reading this page http://tomee.apache.org/jpa-concepts.html

And here is how my code looks like

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);
}

Now running this code, generates the following exception at the line

EntityManager em = emf.createEntityManager();

Here is the exception :

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

So for that I'm not able to create the EntityManager to interact with my database, please clarify to me what I'm missing, thanks in advance.

Upvotes: 0

Views: 1684

Answers (1)

jewelsea
jewelsea

Reputation: 159506

Seems pretty clear from the stack trace that you don't have the Postgres driver on your classpath:

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

The driver is downloadable from postgres.org

Upvotes: 3

Related Questions