我在使用JPA和JavaFx时遇到了一个非常不愉快且不清楚的情况,我试图在另外两个线程中询问这个问题: JPA EntityManager 和 JavaFx 以及 Entity Manager not working in JavaFX
但我不得不在这里打开一个新线程:情况:
我正在使用NetBeans和Postgres、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 来与我的数据库进行交互,请向我澄清我所缺少的,在此先感谢。