我正在从 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.value
和 flux.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
最佳答案
直接实现单元值工厂,而不是使用PropertyValueFactory
(不支持“属性的属性”)。
我假设该表是 TableView<Source>
和fluxColumn
和errorColumn
都是TableColumn<Source, BigDecimal>
。然后你就可以做
fluxColumn.setCellValueFactory(cellData ->
new SimpleObjectProperty<>(cellData.getValue().getFlux().getValue()));
errorColumn.setCellValueFactory(cellData ->
new SimpleObjectProperty<>(cellData.getValue().getFlux().getError()));
如果您使用 JavaFX properties pattern 实现您的实体(并使用 JPA Property Access )然后您可以检索现有属性,而不是每次创建新属性:
fluxColumn.setCellValueProperty(cellData -> cellData.getValue().getFlux().valueProperty());
errorColumn.setCellValueProperty(cellData -> cellData.getValue().getFlux().errorProperty());
关于java - 如何从与另一个 JPA 实体相关的 JavaFX 表中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46197243/