【问题标题】:Using mapped tables (JPA) in javafx在 javafx 中使用映射表 (JPA)
【发布时间】:2015-06-28 08:03:17
【问题描述】:

我已经映射了一张表

import javafx.fxml.FXML;
import org.hibernate.annotations.Immutable;

import javax.persistence.*;
import java.util.List;

@Immutable
@Entity
@Table(schema = "dbo", name = "tCountry")
public class FaCountry {

    @Id
    @Column(name = "CountryID")
    private Long id;
    public Long getId() {
        return id;
    }

    @Column(name = "name")
    private String name;
    public String getName() {
        return name;
    }
}

如何在 JavaFX SceneBuilder 中使用它?我在 SceneBuilder 中创建了一个 TableView,并将这个 TableView 的 fx:id 命名为 countryTable。我想我必须在主类中创建

private ObservableList<FaCountry> countryData = FXCollections.observableArrayList();

并将此 ObservableList 中的信息放入 Controller

@FXML
    private TableView<FaCountry> countryTable;

但是如何将映射表中的数据放入这个 ObservableList 中呢?我无法全面了解如何做到这一点。我对所有这些主题和 Java 都不熟悉。

【问题讨论】:

    标签: hibernate jpa javafx javafx-8 scenebuilder


    【解决方案1】:

    您可以尝试以下方法:

                @FXML
                private TableView<FaCountry> countryTable; 
                private ObservableList<FaCountry> countryData = FXCollections.observableArrayList();
                private EntityManagerFactory emf;
                private EntityManager em;
                //  ...
                    public void initialize(URL url, ResourceBundle rb) {
                    emf = Persistence.createEntityManagerFactory("persistenceTest");
                    em = emf.createEntityManager();
                    TypedQuery<FaCountry> query = em.createQuery(
                            "SELECT e FROM FaCountry e", FaCountry.class);
                    List<FaCountry> list = query.getResultList();
                    countryData.addAll(list);
                    countryTable.setItems(countryData);
        }
    

    【讨论】:

      猜你喜欢
      • 2011-11-06
      • 2013-03-11
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      相关资源
      最近更新 更多