April 05
Prof. Ismael H. F. Santos -  ismael@tecgraf.puc-rio.br                                                          2
Exemplo JPA
nExemplo 2 - JPA
nListagem 01. Bug.java
npackage exemploJPA;
nimport javax.persistence.*;
n@Entity
n@Table(name=“BUGS")
npublic class Bug {
n  private Integer idBug;
n  private String titulo;
n  private java.util.Date data;
n  private String texto;
n  public Bug() {}
n    @Id
n    @GeneratedValue(strategy= 
n                       GenerationType.SEQUENCE)
n    // informa que o id será gerado pelo DB.
n    @Column(name="idBug")
n    public Integer getIdBug(){ return idBug;}  
n    public void setIdBug(Integer iBug) {
n        this.idBug = idBug; 
n    }
n    @Column(name="titulo")
n    public String getTitulo() { return titulo;}
n    public void setTitulo(String titulo){
n      this.titulo = titulo; 
n    }
n    @Temporal(TemporalType.DATE)
n    @Column(name="data")
n    public Date getData(){return data; }
n    public void setData(Date data) {
n        this.data = data; }
n    @Column(name="texto")
n    public String getTexto(){ return texto; }
n    public void setTexto(String texto) {
n        this.texto = texto; } 
n    @Override
n    public String toString(){
n        return "ID: "+this.id_bug; }  
n}
n
nListagem 02. Recuperar objeto.
npublic Object findByPk( int pKey ) {
n        EntityManager em = getEntityManager();
n        return em.find(Bug.class, pKey);
n}