2013-04-07 190 views
5

我是JavaFX的新手,我試圖將一箇舊的Swing應用程序重構爲JavaFX。

我也使用JPA(與EclipseLink實現)來保存/檢索數據。
所以我定義了一些註解的POJO。

這裏我下面的註解POJO:JavaFX,TableView和JPA

import java.io.Serializable; 
import javax.persistence.Basic; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery; 
import javax.persistence.Table; 
import javax.persistence.Transient; 

/** 
* 
* @author alex 
*/ 
@Entity 
@Table(name = "materiale", catalog = "elledia", schema = "") 
public class Materiale implements Serializable { 
    @Transient 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "id") 
    private Long id; 

    @Basic(optional = false) 
    @Column(name = "colata") 
    private String colata; 

    @Basic(optional = false) 
    @Column(name = "dimensione") 
    private String dimensione; 

    @Basic(optional = false) 
    @Column(name = "specifica") 
    private String specifica; 

    @Basic(optional = false) 
    @Column(name = "unita_misura") 
    private String unitaMisura; 

    @Column(name = "version") 
    private Integer version; 

    @ManyToOne (fetch = FetchType.LAZY) 
    @JoinColumn (name = "certificato_id") 
    private Certificato certificato; 

    public Materiale(Long id, String colata, String dimensione, String specifica, String unitaMisura, Certificato certificato) { 
    this.id = id; 
    this.colata = colata; 
    this.dimensione = dimensione; 
    this.specifica = specifica; 
    this.unitaMisura = unitaMisura; 
    this.certificato = certificato; 
    } 

    public Long getId() { 
    return id; 
    } 

    public void setId(Long id) { 
    Long oldId = this.id; 
    this.id = id; 
    } 

    public String getColata() { 
    return colata; 
    } 

    public void setColata(String colata) { 
    String oldColata = this.colata; 
    this.colata = colata; 
    } 

    public String getDimensione() { 
    return dimensione; 
    } 

    public void setDimensione(String dimensione) { 
    String oldDimensione = this.dimensione; 
    this.dimensione = dimensione; 
    } 

    public String getSpecifica() { 
    return specifica; 
    } 

    public void setSpecifica(String specifica) { 
    String oldSpecifica = this.specifica; 
    this.specifica = specifica; 
    } 

    public String getUnitaMisura() { 
    return unitaMisura; 
    } 

    public void setUnitaMisura(String unitaMisura) { 
    String oldUnitaMisura = this.unitaMisura; 
    this.unitaMisura = unitaMisura; 
    } 

    public Integer getVersion() { 
    return version; 
    } 

    public void setVersion(Integer version) { 
    Integer oldVersion = this.version; 
    this.version = version; 
    } 

    public Certificato getCertificato() { 
    return certificato; 
    } 

    public void setCertificato(Certificato certificato) { 
    Certificato oldCertificato = this.certificato; 
    this.certificato = certificato; 
    } 

    @Override 
    public int hashCode() { 
    int hash = 0; 
    hash += (id != null ? id.hashCode() : 0); 
    return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
    // TODO: Warning - this method won't work in the case the id fields are not set 
    if (!(object instanceof Materiale)) { 
     return false; 
    } 
    Materiale other = (Materiale) object; 
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
     return false; 
    } 
    return true; 
    } 

    @Override 
    public String toString() { 
    return "com.alessandrodonato.elledia.certificati.gui.Materiale[ id=" + id + " ]"; 
    } 

} 

我遇到一些麻煩的TableViewcontrol:

  • 好吧,我讀過各種後如何綁定POJO到的TableView。
  • 好的,我必須定義一個ObservableList並將它綁定到TableView來填充和更新它。
  • ,但將其定義我已經使用SimpleStringProperty的字符串,而不是在我的POJO屬性和註釋不符合這方面的工作...

任何想法?

回答

1

TiwulFX提供JavaFX自定義組件專門設計的javaPOJO對象的工作。

+0

謝謝,我會的! – AlexD 2013-04-08 15:16:50

+0

「Java POJO對象」 - 討論冗餘冗餘! :D – ryvantage 2016-10-13 01:36:34

-1
public class MaterialeProperty { 
    private Materiale materiale; 

    public MaterialeProperty (Materiale materiale) { 

     this.materiale= materiale; 

     this.setId(materiale.getId()); 

    } 

    private LongProperty id = new SimpleLongProperty() { 

     public void set(long l) { 
      super.set(l); 
      materiale.setId(l); 
     } 
    }; 

    public final Long getId() { 
     return id.get(); 
    } 

    public final void setId(Long value) { 
     id.set(value); 
    } 

    public LongProperty idProperty() { 
     return id; 
    } 

    .... 

} 
+0

你想說明什麼? – tarrsalah 2013-04-15 15:28:33

+0

該問題的目的確實是如何使用實體作爲POJO,這是如何做到的,通過包裝。 – user2259890 2013-04-17 02:07:12

0

適用於我而不改變我的POJO。

TableColumn coluna1 = new TableColumn("Nome"); 
TableColumn coluna2 = new TableColumn("Álbum"); 
TableColumn coluna3 = new TableColumn("Imagem"); 
TableColumn coluna4 = new TableColumn("Descrição"); 
TableColumn coluna5 = new TableColumn("Tags"); 

coluna1.setCellValueFactory(
    new PropertyValueFactory<>("nome") 
); 
coluna2.setCellValueFactory(
    new PropertyValueFactory<>("album") 
); 

... 

tabelaItens.getColumns().addAll(coluna1, coluna2, coluna3, coluna4, coluna5); 

tabelaItens.setItems(FXCollections.observableArrayList(itens)); 

POJO:

@Entity 
public class Item implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id") 
    private Long id; 

    @ManyToOne 
    private Album album; 

    @Column(name = "nome") 
    private String nome; 

    @Temporal(javax.persistence.TemporalType.TIMESTAMP) 
    @Column(name = "data_criacao") 
    private Calendar dataCriacao; 

    @Column(name = "data_imagem") 
    @Temporal(javax.persistence.TemporalType.TIMESTAMP) 
    private Calendar dataImagem; 

    @Lob 
    @Basic(fetch = FetchType.LAZY) 
    @Column(name = "imagem") 
    private byte[] imagem; 

    @Column(name = "tipo_imagem") 
    private String tipoImagem; 

    @Column(name = "descricao") 
    private String descricao; 

    @Column(name = "tags") 
    private String tags; 

    ... 
}