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.

10
  • You should be able to use this info with a loop or two to accomplish your goal. Commented Nov 23, 2019 at 7:30
  • 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 to extract data about columns and in your cellValueFactorys to retrieve the properties. Commented Nov 23, 2019 at 8:36
  • 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()). Then from that metadata dynamically create and configure appropriate TableColumn instances. Commented Nov 23, 2019 at 11:21
  • 2
    @Slaw might be - but then it's basically unrelated to fx ;) And the basics to learn would be hibernate/jpa .. Commented Nov 23, 2019 at 11:40
  • 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? Commented Nov 26, 2019 at 17:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.