【问题标题】:Loading Custom Objects to JavaFX Combobox from Mysql database从 Mysql 数据库将自定义对象加载到 JavaFX Combobox
【发布时间】:2017-03-02 17:49:19
【问题描述】:

我正在尝试使用 hibernate 和 dao 模式将对象列表从 mysql 表加载到 javaFX Combobox,这是我的代码:

Main.java

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("/view/Home.fxml"));
        Scene scene = new Scene(root,1600,900);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setTitle("Dentium");
        primaryStage.setScene(scene);
        primaryStage.setMaximized(true);

        primaryStage.show();

        Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
        primaryStage.setX((primScreenBounds.getWidth() - primaryStage.getWidth()) / 2);
        primaryStage.setY((primScreenBounds.getHeight() - primaryStage.getHeight()) / 4);

    }catch(Exception e) {
        e.printStackTrace();
    }
}

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

}

OthersDao实现

public class OthersDaoImp implements OthersDao {


@Override
public List<Others> getAllHealthAlerts() {

    List<Others> healthAlerts = new ArrayList<>();
    Transaction transaction = null;
    Session session = HibernateUtil.getSessionFactory().openSession();

    try
    {
        transaction = session.beginTransaction();
        healthAlerts = session.createQuery("from Others").list();
        session.getTransaction().commit();
    }
    catch (RuntimeException e)
    {
        if (transaction != null) {
            transaction.rollback();
        }
        e.printStackTrace();
    }
    finally {
        session.flush();
        session.close();
    }
    return healthAlerts;
}

}

我收到此错误,非常感谢您的帮助。

INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select others0_.id as id1_0_, others0_.healthAlert as        healthAl2_0_ from others others0_
javafx.fxml.LoadException: 
/D:/Programming/EclipseNeon/DentiumFx/bin/view/Home.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3450)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1418)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1414)
at dao.imp.OthersDaoImp.getAllHealthAlerts(OthersDaoImp.java:60)
at controller.PatientController.getHealthAlertsList(PatientController.java:31)
at controller.PatientController.initialize(PatientController.java:115)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more

【问题讨论】:

  • 第 60 行是哪一行?
  • 'session.flush(); ' 的 OthersDaoImp.getAllHealthAlerts();

标签: java hibernate javafx combobox


【解决方案1】:

来自Javadocs for Session.flush()

强制此会话刷新。必须在工作单元结束时调用,在提交事务之前并关闭会话

所以如果你调用flush(),你应该在调用commit()之前在try块中这样做。

但是,flush() 所做的是

synchroniz[e] 底层的持久化存储与内存中的持久化状态

即它使用当前的内存状态更新数据库。由于您没有在此事务中更改内存中的任何持久对象,因此我认为此调用无论如何都是多余的。您应该可以删除此调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-21
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多