2

I'm using hibernate 5.0.7 and JavaFX For UI's.I get a list of data from database,i tried to show them in a tableView,but no thing shown in tableView.

Here is table structure

CREATE TABLE product
(
    idproduct serial NOT NULL,
    namefr character varying(50),
    qtyinhand double precision,
    sellprice double precision,
    CONSTRAINT product_pkey PRIMARY KEY(idproduct)
)

Object Relational Mapping:

package model;

@Entity
@Table(name = "Product")
@Access(AccessType.PROPERTY)
public class Product {
    private LongProperty idProduct;
    private StringProperty nameFr;
    private DoubleProperty qtyInHand;
    private DoubleProperty sellPrice;


    public Product() {
        idProduct = new SimpleLongProperty();
        nameFr = new SimpleStringProperty();
        qtyInHand = new SimpleDoubleProperty();
        sellPrice = new SimpleDoubleProperty();

    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_seq_gen")
    @SequenceGenerator(name = "product_seq_gen", sequenceName = "product_idproduct_seq")
    @Column(name = "idproduct", unique = true, nullable = false)
    public Long getIdProduct() {
        return idProduct.get();
    }

    public LongProperty idProductProperty() {
        return idProduct;
    }

    public void setIdProduct(Long idProduct) {
        this.idProduct.set(idProduct);
    }

    @Column(name = "nameFr")
    public String getNameFr() {
        return nameFr.get();
    }

    public StringProperty nameFrProperty() {
        return nameFr;
    }

    public void setNameFr(String nameFr) {
        this.nameFr.set(nameFr);
    }

    @Column(name = "qtyInHand")
    public double getQtyInHand() {
        return qtyInHand.get();
    }

    public DoubleProperty qtyInHandProperty() {
        return qtyInHand;
    }

    public void setQtyInHand(double qtyInHand) {
        this.qtyInHand.set(qtyInHand);
    }

    @Column(name = "sellPrice")
    public double getSellPrice() {
        return sellPrice.get();
    }

    public DoubleProperty sellPriceProperty() {
        return sellPrice;
    }

    public void setSellPrice(double sellPrice) {
        this.sellPrice.set(sellPrice);
    }
}

I'm using hibernate to retrieve the list of products from database:

public ObservableList<Product> findAll() {

    try {

        session.beginTransaction();
        Query query = session.createSQLQuery("select * from product");
        ObservableList<Product> list = FXCollections.observableArrayList(query.list());

        session.getTransaction().commit();
        session.close();
        return list;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

After that i set the table view to show data:

tcID.setCellValueFactory(new PropertyValueFactory<Product, Long>("idProduct"));
tcNameFR.setCellValueFactory(new PropertyValueFactory("nameFr"));
tcQtyInHand.setCellValueFactory(new PropertyValueFactory("qtyInHand"));
tcSellPrice.setCellValueFactory(new PropertyValueFactory<Product, Double>("sellPrice"));

ProductDAO dao=new ProductDAO();
tableView.getItems().addAll(dao.findAll());

After that i can't get item showed in tablview, instead of that when i debug i notice that dao.findAll()returns a list with size>0,but table don't show any thing.

6
  • Debug the contents of the list and make sure the elements really are Product instances. It's probably better to use a JPQL query here instead of an SQL query, but the SQL version should work (I think...). Assuming you have Products in the list, can you show the rest of the JavaFX code, i.e. creating the table and the columns and displaying the table. Commented Jan 23, 2016 at 21:41
  • The debugger show the list as [Object[X]@Refrence] not [Product[X]@Refrence] Commented Jan 23, 2016 at 21:48
  • i don't really now how to do it in JPQL , i tried Query query = session.createQuery("select t from product t"); but the editor shows that the syntax is incorrect. Commented Jan 23, 2016 at 21:59
  • You need the entity name (Product) not the table name (product). (I should really have said "HQL" instead of "JPQL", since you are using a Hibernate session and not an EntityManager.) Anyway, see answer... Commented Jan 23, 2016 at 22:02
  • Thank's it works. i think , i should revise Hibernate fundamentals Commented Jan 23, 2016 at 22:04

1 Answer 1

1

Since you are using a SQL query, Hibernate doesn't know to associate your entity with the query. You can do

SQLQuery query = session.createSQLQuery("select * from product");
query.addEntity(Product.class);
ObservableList<Product> list = FXCollections.observableArrayList(query.list());

It's probably better to use a HQL query though:

// the really concise, but not very readable "from Product" works as the query too
Query query = session.createQuery("select p from Product as p");
ObservableList<Product> list = FXCollections.observableArrayList(query.list());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.