2017-09-13 132 views
0

我從JPA中的多個表中檢索一些數據。 這裏的第一個實體:如何從與另一個JPA實體相關的JPA實體中檢索JavaFX表的數據?

@Entity 
@Table(name = "source") 
public class Source implements Serializable { 
    @Id 
    @Column(name = "sourceid", nullable = false) 
    private String sourceId; 
    @ManyToOne 
    @JoinTable(name = "flux") 
    private Flux flux; 

    // Other attributes 
    // Constructors, getters and setters 
} 

這裏是第二個實體,即從第一個引用:

@Entity 
@Table(name = "flux") 
public class Flux implements Serializable { 
    @EmbeddedId 
    private FluxId fluxId = new FluxId(); 
    @Column(name = "value") 
    private BigDecimal value; 
    @Column(name = "error") 
    private BigDecimal error; 

    // Other attributes 
    // Constructors, getters and setters 
} 

我想在一個單一的JavaFX表來顯示這些表的數據。怎麼做?這是我正在嘗試做的,但flux.valueflux.error的列總是空的。如何解決這個問題?

// Source table setup 
    sourceIdColumn.setCellValueFactory(new PropertyValueFactory<>("sourceId")); 
    fluxColumn.setCellValueFactory(new PropertyValueFactory<>("flux.value")); // Value of the flux 
    errorColumn.setCellValueFactory(new PropertyValueFactory<>("flux.error")); // Error of the flux 
    // More columns definition from sources attributes 

回答

1

就直接實現單元格的值的工廠,而不是使用PropertyValueFactory(不支持「屬性屬性」)。

我假設表格是TableView<Source>fluxColumnerrorColumn都是TableColumn<Source, BigDecimal>

fluxColumn.setCellValueProperty(cellData -> cellData.getValue().getFlux().valueProperty()); 
errorColumn.setCellValueProperty(cellData -> cellData.getValue().getFlux().errorProperty()); 
:那麼如果使用 JavaFX properties pattern實現你的實體(並使用 JPA Property Access),那麼你可以檢索,而不是創造新的每次現有的屬性,你可以做

fluxColumn.setCellValueFactory(cellData -> 
    new SimpleObjectProperty<>(cellData.getValue().getFlux‌​().getValue())); 
errorColumn.setCellValueFactory(cellData -> 
    new SimpleObjectProperty<>(cellData.getValue().getFlux‌​().getError())); 

0

我建議做一個模型類,例如FluxModel:

public class FluxModel { 

private LongProperty id; 
private ObjectProperty<BigDecimal> value; 
private ObjectProperty<BigDecimal> error; 

    public FluxModel(
      Long id, 
      BigDecimal value, 
      BigDecimal error) { 
     this.id = new SimpleLongProperty(id); 
     this.value = new SimpleObjectProperty<>(value); 
     this.error = new SimpleObjectProperty<>(error); 
    } 

    public long getId() { 
     return id.get(); 
    } 

    public LongProperty idProperty() { 
     return id; 
    } 

    public BigDecimal getValue() { 
     return value.get(); 
    } 

    public ObjectProperty<BigDecimal> valueProperty() { 
     return value; 
    } 

    public BigDecimal getError() { 
     return error.get(); 
    } 

    public ObjectProperty<BigDecimal> errorProperty() { 
     return error; 
    } 
} 

在你的控制器類:

@FXML 
private TableView<FluxModel> table; 
@FXML 
private TableColumn<FluxModel, Long> sourceIdColumn; 
@FXML 
private TableColumn<FluxModel, BigDecimal> fluxColumn; 
@FXML 
private TableColumn<FluxModel, BigDecimal> errorColumn; 

那麼作爲cellValueFactories:

sourceIdColumn.setCellValueFactory(data-> data.getValue().idProperty().asObject()); 
fluxColumn.setCellValueFactory(data -> data.getValue().valueProperty()); 
errorColumn.setCellValueFactory(data -> data.getValue().errorProperty()); 

您可能需要添加StringConverter s到細胞。

然後,你需要一個列表來存儲數據:

private ObservableList<FluxModel> tableData = FXCollections.observableArrayList(); 

你可以填充它,你必須

table.setItems(tableData);