1

I want to populate JavaFX TableView from result of HQL query. I've model class as follow.

package Entity;
// Generated Nov 23, 2019 12:28:42 AM by Hibernate Tools 4.3.1


    import java.util.Date;

    /**
     * Emp generated by hbm2java
     */
    public class Emp  implements java.io.Serializable {

         private int id;
         private String name;
         private String designation;
         private Date dob;

        public Emp() {
        }

        public Emp(int id) {
            this.id = id;
        }
        public Emp(int id, String name, String designation, Date dob) {
           this.id = id;
           this.name = name;
           this.designation = designation;
           this.dob = dob;
        }

        public int getId() {
            return this.id;
        }

        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }
        public String getDesignation() {
            return this.designation;
        }

        public void setDesignation(String designation) {
            this.designation = designation;
        }
        public Date getDob() {
            return this.dob;
        }

        public void setDob(Date dob) {
            this.dob = dob;
        }
    }

I want to create a function which populates the TableView with rows and columns based on result of HQL query.

    @FXML
    private TableView tbl;
    Query myQry = session.createQuery("select e.id, e.name, e.designation, e.dob from Emp e where e.id<10");

    populateTable(myQry);

    public void populateTable(Query qry)
    {
       ???
    }

How can I populate javaFx table based on result of HQL query?

I had already asked this question Here : Dynamically generate columns and rows of javaFX TableView from result of HQL query but someone marked it as Duplicate without reading my question properly. He suggested this answer : JavaFX MySQL connection example please

But this answer is related to SQL not HQL. It neither talks about HQL nor dynamically create columns. My Question is about Dynamically generating columns and rows of javaFX TableView from result of HQL query.

Thanks.

  • You should be able to use [this](https://stackoverflow.com/questions/47885549/how-to-populate-tableview-with-string-arraylist-in-javafx) info with a loop or two to accomplish your goal. – SedJ601 Nov 23 '19 at 07:30
  • Looking at the code you posted, I see why he suggested the duplicate answer. If you database is represented by the Emp Class, then it's best to follow the duplicate answer from your last question. – SedJ601 Nov 23 '19 at 08:01
  • 3
    @Sedrick The solutions for JDBC are not really applicable here: The db is not accessed directly via JDBC, but that part is done by hibernate. You cannot simply convert a hibernate query to an SQL query and even if you could do this, it would be bad practice to open 2 communication channels with the db. Also the duplicate question fails the "dynamic" criterion. Here you probably need to use [`ClassMetadata`](https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/metadata/ClassMetadata.html) to extract data about columns and in your `cellValueFactory`s to retrieve the properties. – fabian Nov 23 '19 at 08:36
  • what is the problem, exactly? you get a list of Emp and you know their generated getters/setters, right? just use them in a valueFactory as usual.. – kleopatra Nov 23 '19 at 09:59
  • 1
    @kleopatra I _think_ ultimately the question is how to read the metadata of an HQL result (i.e. the Hibernate equivalent to JDBC's [`ResultSet#getMetaData()`](https://docs.oracle.com/en/java/javase/13/docs/api/java.sql/java/sql/ResultSet.html#getMetaData())). Then from that metadata dynamically create and configure appropriate `TableColumn` instances. – Slaw Nov 23 '19 at 11:21
  • @Phenom Do you really need such generality? Would it not be enough to use a `TableView`, a `TypedQuery` (at least that's the interface in JPA), and "pre-configure" the `TableColumn` instances? Note you are apparently already selecting every column in the Emp table, so why not leverage the ORM and change the query to actually return `Emp` instances? – Slaw Nov 23 '19 at 11:23
  • 2
    @Slaw might be - but then it's basically unrelated to fx ;) And the basics to learn would be hibernate/jpa .. – kleopatra Nov 23 '19 at 11:40
  • @Slaw u r right. In jdbc, I used ResultSetMetaData() of SQL query to populate JTable in swing. I want to know metadata of HQL query to populate TableView in JavaFX. –  Nov 26 '19 at 07:58
  • 1
    I won't be able to help because I don't know how to get or read metadata in JPA/Hibernate, if that's even possible. However, my previous question still stands: Do you really need this? Since you're using an ORM I find it difficult to believe you are not aware of all possible types you can potentially display. In other words, every query really should have an associated class as the entity/result. In your question you have `Emp` and all information is available ahead-of-time. Again, why not change the query to return a list of `Emp` instances as you're already selecting every column anyway? – Slaw Nov 26 '19 at 17:51
  • @Slaw I want to display only selected columns. Hence number of columns may vary, I didn't understood what you said in last line. If I want to fetch data from multiple Tables ( Classes ) then what should I do? –  Nov 27 '19 at 14:10

0 Answers0