简体   繁体   English

如何使用属性将Hibernate HQL / SQL结果(列表)添加到JavaFX TableView(ObservableList)?

[英]How to add Hibernate HQL/ SQL results (List) to JavaFX TableView (ObservableList) using property?

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. 我正在使用hibernate 5.0.7和JavaFX用于UI。我从数据库中获取了数据列表,我试图在tableView中显示它们,但在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: 我正在使用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;
    }
}

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. 之后,我无法在tablview中显示项目,而是在调试时发现dao.findAll()返回大小大于0的列表,但表未显示任何内容。

Since you are using a SQL query, Hibernate doesn't know to associate your entity with the query. 由于您使用的是SQL查询,因此Hibernate不知道将您的实体与查询相关联。 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: 不过,最好使用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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM