gpt4 book ai didi

java - 如何通过 Hibernate 将 H2 数据库项打印到 JavaFx SceneBuilder 准备的 TableView 上

转载 作者:行者123 更新时间:2023-12-01 13:24:23 28 4
gpt4 key购买 nike

我一直在尝试通过 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com