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;
}