我使用休眠5.0.7和JavaFX对于UI's.I从数据库中获得的数据列表,我想向他们展示在的tableView,但在所示的tableView没有的事。
这里是表结构
CREATE TABLE product
(
idproduct serial NOT NULL,
namefr character varying(50),
qtyinhand double precision,
sellprice double precision,
CONSTRAINT product_pkey PRIMARY KEY(idproduct)
)
对象关系映射:
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);
}
}
我使用Hibernate来检索从数据库产品列表:
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;
}
}
从那以后,我设置表格视图来显示数据:
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());
从那以后,我不能得到实现代码如下所示的项目,而不是当我调试我注意到dao.findAll()返回大小> 0列表,但表不显示任何东西。
由于您使用的是SQL查询,Hibernate不知道你的实体与查询相关联。你可以做
SQLQuery query = session.createSQLQuery("select * from product");
query.addEntity(Product.class);
ObservableList<Product> list = FXCollections.observableArrayList(query.list());
它可能会更好,虽然使用HQL查询:
// 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());