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.
Productinstances. 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 haveProducts in the list, can you show the rest of the JavaFX code, i.e. creating the table and the columns and displaying the table.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...