Databases related to hibernate

0

I made a migration to hibernate a crud and everything went fine until I lacked the last phase where two tables are related; but the problem is that you do not make the relationship.

I made a small relationship, but it does not work when I run it with jsp, it just sends me to the servlet.

This is the code that does not work for me. I already made the SELECT, INSERT, UPDATE :

 try (PrintWriter out = response.getWriter()) {
        Datos dato= new Datos();        
        Cuadro cuadro= new Cuadro(request.getParameter("titulo"),request.getParameter("imagen"));
        Lista lista= new Lista(cuadro,request.getParameter("miLista"));
        dato.guardar(lista);


       /*Constructor JDBC
        dato.ejecutar("Insert into lista (mi_lista, cu_id_fk)"
                + " values ('"+request.getParameter("mi_lista")+"',"
                        + " '"+request.getParameter("cu_id_fk")+"')");*/
        response.sendRedirect("administrador.jsp");   
    }

Update: still sending me to servlet_lista :

public class Servlet_Lista extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            Datos dato= new Datos();        
            Cuadro cuadro= new Cuadro(request.getParameter("titulo"),request.getParameter("imagen"));
            Lista lista= new Lista(cuadro,request.getParameter("miLista"));
            dato.guardar(lista);
           /*Constructor JDBC
            dato.ejecutar("Insert into lista (mi_lista, cu_id_fk)"
                    + " values ('"+request.getParameter("mi_lista")+"',"
                            + " '"+request.getParameter("cu_id_fk")+"')");*/
            response.sendRedirect("administrador.jsp");   
        }
//metodo guardar
 public void guardar(Object dato) {
        SessionFactory sesion = NewHibernateUtil.getSessionFactory();
        Session session;
        session = sesion.openSession();
        Transaction tra = session.beginTransaction();
        session.save(dato);
        tra.commit();
        session.close();
    }

//ser modelo lista
public class Lista  implements java.io.Serializable {


     private Integer idLista;
     private String miLista;
     private Cuadro cuadro;

    public Lista() {
    }

    public Lista(Cuadro cuadro, String miLista) {
       this.cuadro = cuadro;
       this.miLista = miLista;
    }

    public Integer getIdLista() {
        return this.idLista;
    }

    public void setIdLista(Integer idLista) {
        this.idLista = idLista;
    }
    public Cuadro getCuadro() {
        return this.cuadro;
    }

    public void setCuadro(Cuadro cuadro) {
        this.cuadro = cuadro;
    }
    public String getMiLista() {
        return this.miLista;
    }

    public void setMiLista(String miLista) {
        this.miLista = miLista;
    }
    
asked by Kevin Castaño 26.01.2017 в 21:17
source

1 answer

0

Assuming that Data is a class for saving your entities in BD, the form would be as follows:

  try (PrintWriter out = response.getWriter()) {
            Datos dato= new Datos();        
            Cuadro cuadro= new Cuadro(request.getParameter("titulo"),request.getParameter("imagen"));
            Lista lista= new Lista(cuadro,request.getParameter("miLista"));
            dato.guardar(cuadro);
            dato.guardar(lista);

            response.sendRedirect("administrador.jsp"); 
    } 

Remember that if you want the automatic saving of the related tables you must activate the function of save in cascade , this is done in the mapping, whether you do it with JPA or with the mapping in XML.

    
answered by 27.01.2017 в 00:29