无法在JavaFx中创建EntityManager [英] Can't create a EntityManager in JavaFx

查看:133
本文介绍了无法在JavaFx中创建EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常不愉快但不清楚 JPA JavaFx 的情况,我试图在另外两个主题中询问这个问题:




  • 我发现在JavaFx事务类型必须是 RESOURCE_LOCAL ,所以我验证了。

  • 我还发现,使用该模式必须通过@PersistenceUnit注入EntityManagerFactory仅注释(不是@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
* /
公共类Rawda扩展应用
{
@ PersistenceUnit(unitName =RawdaPU)
static private EntityManagerFactory emf;
static
{
try
{
emf = Persistence.createEntityManagerFactory(RawdaPU);
EntityManager em = emf.createEntityManager();
}
catch(例外e)
{
System.out.println(致命:无法创建实体经理工厂);
e.printStackTrace();
}
}

@Override
public void start(阶段阶段)抛出异常
{
父root = FXMLLoader.load(getClass) ().getResource( 查看/ MainView.fxml));

场景场景=新场景(根);

stage.setScene(场景);
stage.show();
}
/ **
*正确部署的JavaFX应用程序中忽略main()方法。
* main()仅用作后备,以防应用程序无法通过部署工件启动
*,例如,在FX
*支持有限的IDE中。 NetBeans忽略main()。
*
* @param args命令行参数
* /
public static void main(String [] args)
{
launch(args) ;
}

现在运行此代码,会在行生成以下异常

  EntityManager em = emf.createEntityManager(); 

以下是例外情况:

 异常描述:配置错误。找不到类[org.postgresql.Driver]。 
致命:无法创建实体管理器工厂
javax.persistence.PersistenceException:异常[EclipseLink-4003](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions。 DatabaseException
异常说明:配置错误。找不到类[org.postgresql.Driver]。
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:517)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDe​​legate.getDatabaseSession(EntityManagerFactoryDe​​legate.java:188)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDe​​legate.createEntityManagerImpl(EntityManagerFactoryDe​​legate.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)
引起:Exception [EclipseLink-4003](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.DatabaseException
异常说明:配置错误。找不到类[org.postgresql.Driver]。
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)
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更多

因此,我无法创建EntityManager来与我的数据库进行交互,请提前告知我我缺少的内容,提前谢谢。

解决方案

从堆栈跟踪中可以清楚地看到你的类路径中没有Postgres驱动程序:

 异常说明:配置错误。找不到类[org.postgresql.Driver]。 

该驱动程序可从 postgres.org


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 :

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

  • I discovered that in JavaFx the transaction-type must be "RESOURCE_LOCAL", so I verified that.
  • I discovered also that with that mode the EntityManagerFactory must be injected via the @PersistenceUnit annotation only (not @PersistenceContext), so I also verified that.

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.

解决方案

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

这篇关于无法在JavaFx中创建EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆