Database 无法在JavaFX的tableView中正确显示数据

Database 无法在JavaFX的tableView中正确显示数据,database,hibernate,javafx,tableview,Database,Hibernate,Javafx,Tableview,我试图在JavaFX应用程序中将数据添加到我的tableView中。我正在使用hibernate对数据库进行操作。我使用一个查询来获取所有订单,并将每个订单存储在一个对象中,然后将该对象添加到tableView的可观察列表中。我创建了orders类并将其映射到我的数据库。这是订单的类别: @Entity @Table(name = "orders") public class orders implements Serializable { @Id @GeneratedValu

我试图在JavaFX应用程序中将数据添加到我的tableView中。我正在使用hibernate对数据库进行操作。我使用一个查询来获取所有订单,并将每个订单存储在一个对象中,然后将该对象添加到tableView的可观察列表中。我创建了orders类并将其映射到我的数据库。这是订单的类别:

@Entity
@Table(name = "orders")
public class orders implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "order_id")
    private int order_id;

    @JoinColumn(name = "item_id")
    @ManyToOne
    @NotNull
    private items item_id;

    @Column(name = "quantity")
    @NotNull
    private int quantity;

    @Column(name = "price_per_unit")
    @NotNull
    private double price_per_unit;

    @Column(name = "total_price")
    @NotNull
    private double total_price;

    @Column(name = "order_date")
    @NotNull
    private Date order_date;

    @JoinColumn(name = "user_id")
    @ManyToOne
    @NotNull
    private users user_id;

    public orders() {
    }

    public orders(int order_id, items item_id, int quantity, double price_per_unit, double total_price, Date order_date, users user_id) {
        this.order_id = order_id;
        this.item_id = item_id;
        this.quantity = quantity;
        this.price_per_unit = price_per_unit;
        this.total_price = total_price;
        this.order_date = order_date;
        this.user_id = user_id;
    }

    public int getOrder_id() {
        return order_id;
    }

    public void setOrder_id(int order_id) {
        this.order_id = order_id;
    }

    public items getItem_id() {
        return item_id;
    }

    public void setItem_id(items item_id) {
        this.item_id = item_id;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double getPrice_per_unit() {
        return price_per_unit;
    }

    public void setPrice_per_unit(double price_per_unit) {
        this.price_per_unit = price_per_unit;
    }

    public double getTotal_price() {
        return total_price;
    }

    public void setTotal_price(double total_price) {
        this.total_price = total_price;
    }

    public Date getOrder_date() {
        return order_date;
    }

    public void setOrder_date(Date order_date) {
        this.order_date = order_date;
    }

    public users getUser_id() {
        return user_id;
    }

    public void setUser_id(users user_id) {
        this.user_id = user_id;
    }



}
下面的代码是视图的代码,其中我有一个tableView,它加载订单并显示数据库中的订单:

public class OrdersPageController implements Initializable {
    private Main app;
    private Session session;
    private Transaction transaction = null;

    @FXML
    private TableView<orders> table;

    public void setApp(Main app) {
        this.app = app;
    }



    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        session = HibernateUtil.getSessionFactory().openSession();
        transaction = session.beginTransaction();

        //Fill the table view
        getOrders();
    }  


    public void goBack(ActionEvent event){
        session.close();
        transaction = null;
        app.goToHomePage();
    }

    public void processLogout(ActionEvent event){
        session.close();
        transaction = null;
        app.userLogout();
    }

    public void addOrder(ActionEvent event){
        session.close();
        transaction = null;
        app.addOrdersPage();
    }

    public void deleteOrder(ActionEvent event){
        session.close();
        transaction = null;
        app.closeOrdersPage();
    }

    public void getOrders(){

        try{

            String hql = "FROM orders";
            Query query = session.createQuery(hql);
            List<orders> list = query.getResultList();

            for (orders o : list) {
                //Create an order object
                orders order = new orders();
                order.setOrder_id(o.getOrder_id());
                order.setItem_id(o.getItem_id());
                order.setPrice_per_unit(o.getPrice_per_unit());
                order.setQuantity(o.getQuantity());
                order.setOrder_date(o.getOrder_date());
                order.setTotal_price(o.getTotal_price());
                order.setUser_id(o.getUser_id());

                //Create an observable list for the table
                ObservableList<orders> tableList = table.getItems();
                //Add the order object to the list
                tableList.add(order);
                //Set the created list to the table to show data
                table.setItems(tableList);
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }

    }

}
请注意,getOrders方法是从数据库获取订单并设置tableView的可观察列表的方法

我在显示订单的项目id和用户id时遇到问题。我认为问题在于,它们分别是items和users类型的对象,表中显示了对象的地址。相反,我想显示订购物品的ID号和下订单的用户。如果您知道我可以做些什么来解决我的问题,请与我分享。

只需在users和items类中重写toString方法:

示例:在用户类中->

正如James_D所说的,看看java约定。Java类应始终使用大写字母。

将CellFactory添加到相关列中。您在问题中没有显示FXML,因此我不知道您分配给相应TableColumn实例的名称,但您可以执行以下操作:

public class OrdersPageController implements Initializable {

    // ...

    @FXML
    private TableView<orders> table;

    @FXML
    private TableColumn<orders, users> userColumn ;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        userColumn.setCellFactory(tc -> new TableCell<>() {
            @Override
            protected void updateItem(users user, boolean empty) {
                super.updateItem(user, empty);
                if (empty || user == null) {
                    setText("");
                } else {
                    String text = /* anything you need based on user */
                    setText(text);
                }
            }
        });

        session = HibernateUtil.getSessionFactory().openSession();
        transaction = session.beginTransaction();

        //Fill the table view
        getOrders();
    }  
}

使用单元工厂生成按需显示对象的单元。显示如何格式化数字,但您可以使用相同的技术将表格单元格中的文本设置为从该单元格的数据中获得的所需的任何值;请使用-它将使代码格式化程序能够正确地突出显示您的代码,例如类名,使Java程序员更容易阅读,而且由于我猜您正在为单元格值工厂使用PropertyValueFactorys,因此在JavaFX之类的库中使用时,问题会少一些。这是一个非常糟糕的建议。您可能需要toString方法用于其他目的,特别是用于调试,而这些目的与您希望在UI中显示数据的方式无关。我认为这是他代码中最简单的解决方案。真糟糕,你刚刚否决了它!如果我们在讨论什么是好的,什么是不好的,那么让一个名为user_id的变量从users类型开始就毫无意义。如果您在model类中设计方法,则会破坏将数据从UI中分离出来的目的。toString方法将特定于UI显示数据的方式。向下投票不是针对个人的,我只是试图阻止未来用户使用次优解决方案。出于应用程序的原因而覆盖tostring显然是错误的,正如@James_D已经指出的那样:只需考虑在不同的视图中以不同的要求可视化特定的对象。。
public class OrdersPageController implements Initializable {

    // ...

    @FXML
    private TableView<orders> table;

    @FXML
    private TableColumn<orders, users> userColumn ;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        userColumn.setCellFactory(tc -> new TableCell<>() {
            @Override
            protected void updateItem(users user, boolean empty) {
                super.updateItem(user, empty);
                if (empty || user == null) {
                    setText("");
                } else {
                    String text = /* anything you need based on user */
                    setText(text);
                }
            }
        });

        session = HibernateUtil.getSessionFactory().openSession();
        transaction = session.beginTransaction();

        //Fill the table view
        getOrders();
    }  
}