从Mysql数据库将自定义对象加载到JavaFX Combobox

时间:2017-03-02 17:49:20

标签: java hibernate javafx combobox

我正在尝试使用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);
}

}

OthersDaoImplementation

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

1 个答案:

答案 0 :(得分:0)

来自Javadocs for Session.flush()

  

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

因此,如果您致电flush(),则应在调用try之前在commit()块中执行此操作。

然而,flush()做的是

  

synchroniz [e]具有持久状态的底层持久存储在内存中

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