如何从另一个JPA实体中检索JavaFX表的数据?

时间:2017-09-13 12:11:48

标签: java jpa javafx

我正在从JPA中的多个表中检索一些数据。 这是第一个实体:

@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

2 个答案:

答案 0 :(得分:1)

直接实现单元格值工厂,而不是使用# -------------------------------- # Turn on the Mouse Support - defaults seem good # -------------------------------- set-option -g mouse on # when we finish "selecting" send it to pbcopy (and into the OS X buffer) bind-key -t vi-copy MouseDragEnd1Pane copy-pipe "pbcopy" # -------------------------------- # Use vim keybindings in copy mode # -------------------------------- setw -g mode-keys vi # Setup 'v' to begin selection as in Vim # You enter with C-b [ and then "v" - then normal keypresses to "highlight" # .. [Enter] or "y" will select (because of below bindings) bind-key -t vi-copy v begin-selection # # 'y'ank will send the selection to the OS X buffer bind-key -t vi-copy y copy-pipe "pbcopy" # -------------------------------- # Update default binding of `Enter` to also use Send the selection to OS X buffer # -------------------------------- unbind -t vi-copy Enter bind-key -t vi-copy Enter copy-pipe "pbcopy" # selecting can now be done with # hilighting with a mouse # selecting with C-b [ v .. now vi mode for selecting text # # pasting can now be done with # ⌘ - V # C-b ] (不支持“属性属性”)。

我认为该表格为PropertyValueFactory,而TableView<Source>fluxColumn均为errorColumn。然后就可以了

TableColumn<Source, BigDecimal>

如果您使用JavaFX properties pattern实施实体(并使用JPA Property Access),那么您可以检索现有属性,而不是每次都创建新属性:

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

答案 1 :(得分: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添加到单元格中。

然后你需要一个列表来存储数据:

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

你可以填充它,你必须

table.setItems(tableData);