title: How to print H2 database items onto a JavaFx SceneBuilder prepared TableView via Hibernate
body: <p>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.</p>
  
  <p>This is my Controller Class:</p>
  
  <pre><code>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&lt;NewBeautifulKiwi&gt; KIWI_TABLE;
  
      @FXML
      private TableColumn&lt;NewBeautifulKiwi, Integer&gt; KiwiId;
  
      @FXML
      private TableColumn&lt;NewBeautifulKiwi, String&gt; Kiwi;
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
          System.out.println("Now we print onto out onto our TableView");
          KiwiId.setCellValueFactory(new PropertyValueFactory&lt;NewBeautifulKiwi, Integer&gt;("KiwiId"));
          Kiwi.setCellValueFactory(new PropertyValueFactory&lt;NewBeautifulKiwi, String&gt;("Kiwi"));
          KIWI_TABLE.getItems().setAll(gobbledyGook());
  
      }
  
      private ObservableList&lt;NewBeautifulKiwi&gt; gobbledyGook() {
          ObservableList&lt;NewBeautifulKiwi&gt; 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;
      }
  
  }
  </code></pre>
  
  <p>The FXML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;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"&gt;
    &lt;children&gt;
      &lt;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"&gt;
        &lt;columns&gt;
          &lt;TableColumn prefWidth="75.0" text="KiwiId" fx:id="KiwiId" /&gt;
          &lt;TableColumn prefWidth="75.0" text="Kiwi" fx:id="Kiwi" /&gt;
        &lt;/columns&gt;
      &lt;/TableView&gt;
    &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>The POJO Class:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>Hibernate.cfg:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
  
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;org.h2.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:h2:file:C:/WAKILI/WAKILIdb&lt;/property&gt;
          &lt;property name="connection.username"&gt;sa&lt;/property&gt;
          &lt;property name="connection.password"&gt;&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
  
          &lt;!-- Enable Hibernate's automatic session context management --&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
      &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
  
  
  
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;!-- Drop and re-create the database schema on startup --&gt;
          &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
  
          &lt;!-- Names the annotated entity class --&gt;
          &lt;mapping class="tableviewfix.NewBeautifulKiwi"/&gt;
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>If the @GeneratedValue was being incremented right, I should get the above output, but I instead get this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>ALL ITEMS GET a @GeneratedValue of 0.</p>
  
  <p>The relevant class that would do the database entry looks as follows:</p>
  
  <pre><code>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());
      }
  }
  </code></pre>