恐怖的Hibernate和JavaFX Table CallBack!

目錄 [隱藏]javascript

Hibernate

最近在作 JavaFX 應用,無論再怎麼避免數據持久化,但面對幾十萬的數據量的時候也只能乖乖的去配置持久層框架了。php

一開始打算仍是使用Mybatis,我在作Web的時候就是一隻用Mybatis框架,而Mybatis-plus的強大構造器用着也很是順手,但是JavaFX若是使用Mybatis-plus就必需要代碼生成器,那太麻煩了,不幹。java

而mybatis原生框架又太原生了,加上配置出一肚子火,換Hibernate!mysql

雖然以前沒用過,但Hibernate早在我剛學Java的時候就已是如雷灌耳了,並且是一個自帶Sql構造器的全自動持久層框架,以爲比Mybatis更加穩定。sql

接着就是配置,Maven中引入:數據庫

<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.2.Final</version> </dependency>

接着是XML配置文件:session

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 數據庫方言 --> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property><!-- 引用jdbc包 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bbs?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true</property> <!-- 數據庫連接 --> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">1234</property> <!-- <property name="hibernate.connection.username">root</property>--> <!-- <property name="hibernate.connection.password">1234</property>--> <property name="hibernate.format_sql">true</property> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration>

至於實體類和映射文件直接用IDEA生成,太特麼爽了!mybatis

接着,兩個異常讓我搞了半天。app

1.no entity found for query框架

2.MappingNotFoundException: resource:**.hbm.xml not found

網上了博客答案沒什麼新意,解決不了個人問題。

最終,第一個問題是:

List<HupuOriginEntity> hupuOriginEntities = session.createQuery("from xyz.chaojie.db.HupuOriginEntity").list();

要加包名。

第二個:

Entity.hbm.xml 文件放在resouce文件夾中!

 

JavaFX Table

我只是想讓JavaFX的Table多一個操做列,加一個能夠刪除一行數據的按鈕。

這代碼量和邏輯令我十分懼怕。

Callback<TableColumn<HupuOriginEntity, String>, TableCell<HupuOriginEntity, String>> callback = new Callback<TableColumn<HupuOriginEntity, String>, TableCell<HupuOriginEntity, String>>() { @Override public TableCell<HupuOriginEntity, String> call(TableColumn<HupuOriginEntity, String> param) { Button jfxButton = new Button("刪除"); jfxButton.getStyleClass().add("danger"); return new TableCell<HupuOriginEntity, String>() { Button btn = jfxButton; @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (empty) { setGraphic(null); setText(null); } else { btn.setOnAction(event -> { Dialog dialog = new Dialog( DialogType.CONFIRMATION, DialogStyle.UNDECORATED, "信息", "肯定刪除?"); dialog.showAndWait(); HupuOriginEntity delEntity = getTableView().getItems().get(getIndex()); if (dialog.getResponse() == DialogResponse.YES) { Transaction transaction = session.beginTransaction(); session.delete(delEntity); transaction.commit(); reloadTable(); } }); setGraphic(btn); setText(null); } } }; } };
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息