Lenar a JTable with JPA

0

I'm new to this JPA and I know almost nothing about it, although I've researched a bit, I still can not find a way to fill the table with the Entity , the code with which I do a DefaultTableModel and try to fill the table with this is the following:

public JTable listarTabla(JTable tabla) {
    DefaultTableModel modelo = new DefaultTableModel();
    tabla.setModel(modelo);

    tabla = new javax.swing.JTable() {
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    };

    Object[] columnas = new Object[3];

    modelo.addColumn("NOMBRE");
    modelo.addColumn("APELLIDO");
    modelo.addColumn("MOVIL");

    int objetos = agendaJPA.extraerTodos().size();

    for (int i = 0; i < objetos; i++) {
        columnas[0] = this.agendaJPA.extraerTodos().get(i).getNombre();
        columnas[1] = this.agendaJPA.extraerTodos().get(i).getApellido();
        columnas[2] = this.agendaJPA.extraerTodos().get(i).getMovil();

        modelo.addColumn(columnas);
    }

    return tabla;
}

The extraerTodos() method that I use to fill the table is this:

public class AgendaJPADAOImpl implements AgendaJPADAO, Serializable {

EntityManagerFactory emf = Persistence.createEntityManagerFactory("AGENDAJPAPU");
EntityManager em = emf.createEntityManager();

 public List<Contacto> extraerTodos() {
    List<Contacto> contactos;
    contactos = em.createNamedQuery("AgendaJPA.getAll").getResultList();

    return contactos;
    }
}

In turn, the extraerTodos() method refers to the name of this JPQL code within the POJO

package comm.agendaJPA.modelo;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Calendar;

@Entity(name = "ContactoAgenda")
@Table(name = "contacto")
@NamedQueries({
    @NamedQuery(name = "AgendaJPA.getAll", query = "SELECT c FROM ContactoAgenda c")
})
public class Contacto implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "fecha")
@Temporal(TemporalType.TIMESTAMP)
private Calendar fecha;
@Column(name = "nombre")
private String nombre;
@Column(name = "apellido")
private String apellido;
@Column(name = "movil")
private Integer movil;

public Contacto() {
}

public Contacto(String nombre, String apellido, int movil, int casa, String personal, String trabajo, String direccion) {
    this.nombre = nombre;
    this.apellido = apellido;
    this.movil = movil;
    this.casa = casa;
    this.personal = personal;
    this.trabajo = trabajo;
    this.direccion = direccion;
}

public Integer getId() {
    return id;
}

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

public Calendar getFecha() {
    return fecha;
}

public void setFecha(Calendar fecha) {
    this.fecha = fecha;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public String getApellido() {
    return apellido;
}

public void setApellido(String apellido) {
    this.apellido = apellido;
}

public Integer getMovil() {
    return movil;
}

public void setMovil(Integer movil) {
    this.movil = movil;
}

By running the program I get these results:

    
asked by David Calderon 20.10.2016 в 22:14
source

1 answer

1

I leave this example like mapping a tabla in JPA ROLL CLASS

@Entity
@Table(name = "rol")

public class Rol implements 
Serializable {

@Id
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "descripcion")
private String descripcion;

public Rol() {
}

public Rol(Integer id) {
    this.id = id;
}

public Rol(Integer id, String descripcion) {
    this.id = id;
    this.descripcion = descripcion;
}

public Integer getId() {
    return id;
}

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

public String getDescripcion() {
    return descripcion;
}

public void setDescripcion(String descripcion) {
    this.descripcion = descripcion;
}

}




public void modeloTabla() {
    List<Rol> listado = listarTodos();
    /*coloco el nombre de las  columnas de la tabla Rol a el modelo */
    DefaultTableModel model = new DefaultTableModel(null, new Object[]{"ID", "DESCRIPCION"});
    for (Rol r : listado) {
        model.addRow(new Object[]{r.getId(), r.getDescripcion()});
    }
    /*establecemos el modelo  al Jtable llamado jTabla*/
    jTabla.setModel(model);
}




 public List<Rol> listarTodos() {
    List<Rol> datos;
    /*la unidad de persistencia del archivo Persistence.xml se llama unidadPersistencia*/
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("unidadPersistencia");
    EntityManager em = emf.createEntityManager();
    Query q = em.createQuery("SELECT r FROM Rol r");
    datos = q.getResultList();
    return datos;
}
    
answered by 07.11.2016 в 20:07