I've been trying to print H2 database items onto a JavaFx SceneBuilder prepared TableView via Hibernate, but have been failing miserably. Please help me out figure where I'm going wrong.
This is my Controller Class:
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;
}
}
The FXML file:
<?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>
The POJO Class:
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>
EDIT:
I just realised that all new entries get a @GeneratedValue of 0. I think this is where the problem is. How can I have new entries assigned increasing numbers, like:
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
If the @GeneratedValue was being incremented right, I should get the above output, but I instead get this:
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
ALL ITEMS GET a @GeneratedValue of 0.
The relevant class that would do the database entry looks as follows:
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());
}
}