28/10/2014, 05:50
|
| | Fecha de Ingreso: septiembre-2014 Ubicación: guanare edo portuguesa
Mensajes: 15
Antigüedad: 10 años, 3 meses Puntos: 0 | |
Respuesta: Insertar Varios Registros en una Tabla Jpa Bien: aca estan los codigos. gracias
"codigo como recuperas la lista de datos"
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Controlador;
import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Damaso Montilla
*/
@WebServlet(name = "familia", urlPatterns = {"/familia"})
public class familia extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nombrefamiliar = request.getParameter("nombrefamiliar");
String apellidofamiliar = request.getParameter("apellidofamiliar");
String cedulafamiliar = request.getParameter("cedulafamiliar");
String edadfamiliar = request.getParameter("edadfamiliar");
String estudiosfamiliar = request.getParameter("estudiosfamiliar");
String parentescofamiliar = request.getParameter("parentescofamiliar");
String cedula = request.getParameter("cedula");
Modelo.Familiar familia = new Modelo.Familiar();
familia.setIdfamiliar(0);
familia.setNombrefamiliar(nombrefamiliar);
familia.setApellidofamiliar(apellidofamiliar);
familia.setCedulafamiliar(cedulafamiliar);
familia.setEdadfamiliar(edadfamiliar);
familia.setEstudiosfamiliar(estudiosfamiliar);
familia.setParentescofamiliar(parentescofamiliar);
familia.setCedula(cedula);
EntityManager em;
EntityManagerFactory emf;
emf= Persistence.createEntityManagerFactory("Registro_E sinsep_TrabajadoresPU");
em = emf.createEntityManager();
em.getTransaction().begin();
em.persist (familia);
em.flush();
em.getTransaction().commit();
response.sendRedirect("Correcto1.jsp");
"como la insertas en la base de datos"
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Modelo;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Damaso Montilla
*/
@Entity
@Table(name = "familiar")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Familiar.findAll", query = "SELECT f FROM Familiar f"),
@NamedQuery(name = "Familiar.findByIdfamiliar", query = "SELECT f FROM Familiar f WHERE f.idfamiliar = :idfamiliar"),
@NamedQuery(name = "Familiar.findByCedulafamiliar", query = "SELECT f FROM Familiar f WHERE f.cedulafamiliar = :cedulafamiliar"),
@NamedQuery(name = "Familiar.findByNombrefamiliar", query = "SELECT f FROM Familiar f WHERE f.nombrefamiliar = :nombrefamiliar"),
@NamedQuery(name = "Familiar.findByApellidofamiliar", query = "SELECT f FROM Familiar f WHERE f.apellidofamiliar = :apellidofamiliar"),
@NamedQuery(name = "Familiar.findByEdadfamiliar", query = "SELECT f FROM Familiar f WHERE f.edadfamiliar = :edadfamiliar"),
@NamedQuery(name = "Familiar.findByEstudiosfamiliar", query = "SELECT f FROM Familiar f WHERE f.estudiosfamiliar = :estudiosfamiliar"),
@NamedQuery(name = "Familiar.findByParentescofamiliar", query = "SELECT f FROM Familiar f WHERE f.parentescofamiliar = :parentescofamiliar"),
@NamedQuery(name = "Familiar.findByCedula", query = "SELECT f FROM Familiar f WHERE f.cedula = :cedula")})
public class Familiar implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idfamiliar")
private Integer idfamiliar;
@Basic(optional = false)
@Column(name = "cedulafamiliar")
private String cedulafamiliar;
@Basic(optional = false)
@Column(name = "nombrefamiliar")
private String nombrefamiliar;
@Basic(optional = false)
@Column(name = "apellidofamiliar")
private String apellidofamiliar;
@Basic(optional = false)
@Column(name = "edadfamiliar")
private String edadfamiliar;
@Basic(optional = false)
@Column(name = "estudiosfamiliar")
private String estudiosfamiliar;
@Basic(optional = false)
@Column(name = "parentescofamiliar")
private String parentescofamiliar;
@Basic(optional = false)
@Column(name = "cedula")
private String cedula;
public Familiar() {
}
public Familiar(Integer idfamiliar) {
this.idfamiliar = idfamiliar;
}
public Familiar(Integer idfamiliar, String cedulafamiliar, String nombrefamiliar, String apellidofamiliar, String edadfamiliar, String estudiosfamiliar, String parentescofamiliar, String cedula) {
this.idfamiliar = idfamiliar;
this.cedulafamiliar = cedulafamiliar;
this.nombrefamiliar = nombrefamiliar;
this.apellidofamiliar = apellidofamiliar;
this.edadfamiliar = edadfamiliar;
this.estudiosfamiliar = estudiosfamiliar;
this.parentescofamiliar = parentescofamiliar;
this.cedula = cedula;
}
public Integer getIdfamiliar() {
return idfamiliar;
}
public void setIdfamiliar(Integer idfamiliar) {
this.idfamiliar = idfamiliar;
}
public String getCedulafamiliar() {
return cedulafamiliar;
}
public void setCedulafamiliar(String cedulafamiliar) {
this.cedulafamiliar = cedulafamiliar;
}
public String getNombrefamiliar() {
return nombrefamiliar;
}
public void setNombrefamiliar(String nombrefamiliar) {
this.nombrefamiliar = nombrefamiliar;
}
public String getApellidofamiliar() {
return apellidofamiliar;
}
public void setApellidofamiliar(String apellidofamiliar) {
this.apellidofamiliar = apellidofamiliar;
}
public String getEdadfamiliar() {
return edadfamiliar;
}
public void setEdadfamiliar(String edadfamiliar) {
this.edadfamiliar = edadfamiliar;
}
public String getEstudiosfamiliar() {
return estudiosfamiliar;
}
public void setEstudiosfamiliar(String estudiosfamiliar) {
this.estudiosfamiliar = estudiosfamiliar;
}
public String getParentescofamiliar() {
return parentescofamiliar;
}
public void setParentescofamiliar(String parentescofamiliar) {
this.parentescofamiliar = parentescofamiliar;
}
public String getCedula() {
return cedula;
}
public void setCedula(String cedula) {
this.cedula = cedula;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idfamiliar != null ? idfamiliar.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 Familiar)) {
return false;
}
Familiar other = (Familiar) object;
if ((this.idfamiliar == null && other.idfamiliar != null) || (this.idfamiliar != null && !this.idfamiliar.equals(other.idfamiliar))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Modelo.Familiar[ idfamiliar=" + idfamiliar + " ]";
}
} |