- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试通过 Hibernate 将 H2 数据库项目打印到 JavaFx SceneBuilder 准备的 TableView 上,但一直失败得很惨。请帮我找出哪里出错了。
这是我的 Controller 类:
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class FXMLDocumentController implements Initializable {
@FXML
private TableView<NewBeautifulKiwi> KIWI_TABLE;
@FXML
private TableColumn<NewBeautifulKiwi, Integer> KiwiId;
@FXML
private TableColumn<NewBeautifulKiwi, String> Kiwi;
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("Now we print onto out onto our TableView");
KiwiId.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, Integer>("KiwiId"));
Kiwi.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, String>("Kiwi"));
KIWI_TABLE.getItems().setAll(gobbledyGook());
}
private ObservableList<NewBeautifulKiwi> gobbledyGook() {
ObservableList<NewBeautifulKiwi> data;
data = FXCollections.observableArrayList();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
List courses = session.createQuery("from KIWI_TABLE").list();
for (Iterator iterator = courses.iterator(); iterator.hasNext();) {
NewBeautifulKiwi course = (NewBeautifulKiwi) iterator.next();
System.out.println(course.getKiwi());
data.add(course);
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return data;
}
}
FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="293.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="tableviewfix.FXMLDocumentController">
<children>
<TableView fx:id="KIWI_TABLE" prefHeight="293.0" prefWidth="320.0" tableMenuButtonVisible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="KiwiId" fx:id="KiwiId" />
<TableColumn prefWidth="75.0" text="Kiwi" fx:id="Kiwi" />
</columns>
</TableView>
</children>
</AnchorPane>
POJO 类:
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity(name = "KIWI_TABLE")
public class NewBeautifulKiwi implements Serializable {
@Id
@GeneratedValue
private int KiwiId;
private String Kiwi;
public int getKiwiId() {
return KiwiId;
}
public void setKiwiId(int KiwiId) {
this.KiwiId = KiwiId;
}
public String getKiwi() {
return Kiwi;
}
public void setKiwi(String Kiwi) {
this.Kiwi = Kiwi;
}
}
Hibernate.cfg:
<?xml version='1.0' encoding='utf-8'?>
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:file:C:/WAKILI/WAKILIdb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Names the annotated entity class -->
<mapping class="tableviewfix.NewBeautifulKiwi"/>
</session-factory>
</hibernate-configuration>
编辑:
我刚刚意识到所有新条目的 @GeneeratedValue 均为 0。我认为这就是问题所在。如何为新条目分配越来越多的数字,例如:
This is Entry Number: First Entry - and the @GeneratedValue Number is: 0
This is Entry Number: Second Entry - and the @GeneratedValue Number is: 1
This is Entry Number: Third Entry - and the @GeneratedValue Number is: 2
This is Entry Number: Fourth Entry - and the @GeneratedValue Number is: 3
如果 @GenerateValue 正确递增,我应该得到上面的输出,但我得到的是:
This is Entry Number: First Entry - and the @GeneratedValue Number is: 0
This is Entry Number: Second Entry - and the @GeneratedValue Number is: 0
This is Entry Number: Third Entry - and the @GeneratedValue Number is: 0
This is Entry Number: Fourth Entry - and the @GeneratedValue Number is: 0
所有项目的 @GeneeratedValue 均为 0。
执行数据库条目的相关类如下所示:
public class PersistNewBeautifulKiwi {
public void doKiwi(String kiwi) {
NewBeautifulKiwi newBeautifulKiwi = new NewBeautifulKiwi();
newBeautifulKiwi.setKiwi(kiwi);
HibernateUtil.getSessionFactory();
System.out.println("\n" + "This is Entry Number: " + newBeautifulKiwi.getKiwi() + " - and the @GeneratedValue Number is: " + newBeautifulKiwi.getKiwiId());
}
}
最佳答案
为上述指定一个生成器
@Id
@GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
@Column(name="CUST_ID")
public Long getId() { return id; }
Example 2:
@Id
@GeneratedValue(strategy=TABLE, generator="CUST_GEN")
@Column(name="CUST_ID")
Long id;
这在这里得到了很好的解释:Hibernate: rundown on how @GeneratedValue works
关于java - 如何通过 Hibernate 将 H2 数据库项打印到 JavaFx SceneBuilder 准备的 TableView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21849617/
我是 JavaFX 的新手,我还没有找到关于这个问题的任何教程/答案。 所以基本上我想做的只是改变我场景的一部分。我想在顶部保留一个静态菜单栏,仅根据单击的菜单按钮更改底部。这意味着我需要为每个“页面
在我的 JavaFX 应用程序中,某些节点显示在预览中,但不可选择,也不会显示在层次结构中。 SceneBuilder 不可定位示例: 在此示例中,“名字”列不可定位,并且不会显示在层次结构中,即使它
我在使用场景生成器时遇到了奇怪的渲染问题,但前提是我从 ControlsFX 导入了 JAR。 正如您在以下视频中看到的: https://www.youtube.com/watch?v=dD1euL
我创建了这个自定义组件: public class IconButton extends Button { @FXML private ImageView imageView; pri
我下载了 Scene Builder,并尝试打开 .fxml 文件,但出现以下错误消息: Failed to launch SceneBuilder. The error message was: C
我刚刚从 Gluon 安装了 SceneBuilder,但无法启动它...它安装正确(我注意到安装程序没有到达末尾...但所有文件都在 AppData/Local 中正确)我尝试启动 EXE 但它不会
我安装了 SceneBuilder 8.3.0在安装了Java Hotspot 1.8.0_131的Ubuntu Gnome 17.04上,尝试启动时出现错误,无法初始化。我已经尝试安装Oracle的
不确定这是不是该问的地方。如果有更好的 SE 站点,请告诉我。 自从我开始使用 SceneBuilder 以来,它的子菜单就完全无法使用,因为一旦我移动光标选择一个选项,它们就会消失。 例如 - 假设
我对 SceneBuilder 和 Java FX 处理自定义控件的方式束手无策。 我是使用 fx:root,还是不使用它?我选择使用它。所以在控件的Controller+Root类,我把自己设置为r
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
在 JavaFX 中存在控件 ToggleGroup 。我已经安装了SceneBuilder,版本: Product Version JavaFX Scene Builder 8.0.0 Build
我的问题是,如何在 JavaFX 中根据应用程序窗口的大小实现在操作过程中自动调整图像大小?这可以直接在 SceneBuilder 中完成吗? 最佳答案 它将其 WidthProperty 和 Hei
我需要用 SceneBuilder 创建一个 GUI。我在我的界面中添加了一个 ImageView 并正确设置了我的图像的路径。图像显示在 SceneBuilder 中,但是当我运行我的应用程序时,图
这就是我正在尝试做的事情: 我想要一个显示 31 个 Pane 的场景,然后将用红色或绿色填充它们。 (点击它们应该改变颜色) 我现在想知道实现这一点的最佳方法是什么。我首先想到了 TableView
我在 SceneBuilder 打开我的 main.fxml 时遇到问题。当我运行项目时一切正常,但我无法在 SceneBuilder 下进行修改。每次我想添加到 main.fxml 时,我都需要删除
我正在使用 SceneBuilder 构建 javaFX 应用程序,问题是,当我在 fxml 文件中指定相对路径时,它会在运行时抛出异常: null/../images/text_formal.png
我正在尝试使用场景生成器创建的按钮切换场景。 这是我的 Controller 类 public class Controller implements Initializable { @FXM
我有一个非常简单的类,它获取作业列表并尝试用它们填充 TableView。我在不使用 FXML 的情况下完成了这项工作,但我现在正在尝试新方法。我遇到的问题是; tView.getItems().ad
我试图在 SceneBuilder 中重新创建库列表的外观,但我不知道我需要采用哪个元素。 如何重新创建此列表? 最佳答案 这是使用 ControlsFx Awesome Fonts 的草稿。 Mai
我希望有人能够向我解释为什么 SceneBuilder 在导入自定义控件时如此不稳定。 以一个相对简单的自定义控件为例(仅作为示例): public class HybridControl exten
我是一名优秀的程序员,十分优秀!