Example usage for org.hibernate Session flush

List of usage examples for org.hibernate Session flush

Introduction

In this page you can find the example usage for org.hibernate Session flush.

Prototype

void flush() throws HibernateException;

Source Link

Document

Force this session to flush.

Usage

From source file:CriarProduto.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  w w  w. java2  s. co  m
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet CriarProduto</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet CriarProduto at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");

        Produto pteste = new Produto();
        pteste.setFabricante("pilot");
        pteste.setNome("bonina");
        pteste.setPreco(1);
        // pteste.setId(0); n~ precisa esta sendo gerado automaticamente

        //conectar com o banco
        Session sessao = HibernateUtil.getSessionFactory().openSession();
        //criar ponto de restauraao, aguarda os dados na memoria sem persistir no banco
        Transaction tx = sessao.beginTransaction();
        //
        sessao.save(pteste);
        sessao.flush();

        tx.commit();
        sessao.close();
    }
}

From source file:criarEquipe.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w  w  w. j  av a2s .c  om
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        String n = request.getParameter("nome");
        String t = request.getParameter("treinador");
        String m = request.getParameter("mascote");

        Equipe e;
        e = new Equipe();

        e.setNome(n);
        e.setTreinador(t);
        e.setMascote(m);

        Session sessao;
        sessao = HibernateUtil.getSessionFactory().openSession();

        Transaction tx = null;
        try {
            tx = sessao.beginTransaction();

            sessao.save(e);
            sessao.flush();

            tx.commit();
            sessao.close();
        } catch (Exception ex) {
            System.out.println("Deu erro: " + ex.getMessage());
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }

        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet criarEquipe</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet criarEquipe at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:Incluir.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   w w  w .j  ava2s .c  o m
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        String Smatriculas = request.getParameter("matricula");
        String nome = request.getParameter("nome");
        String Shoras = request.getParameter("horas");
        String Spousos = request.getParameter("pousos");

        int matricula = parseInt(Smatriculas);
        int horas = parseInt(Shoras);
        int pousos = parseInt(Spousos);

        Session sessao = HibernateUtil.getSessionFactory().openSession();

        Transaction t = sessao.beginTransaction();

        Aeronave a = new Aeronave();
        a.setMatricula(matricula);
        a.setNome_aeronave(nome);
        a.setHoras_de_voo(horas);
        a.setQuantidade_pousos(pousos);

        sessao.save(a);
        sessao.flush();
        t.commit();
        sessao.close();

        out.println("registrado");

    }
}

From source file:editarProfessor.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* w  w w. j a  v  a  2  s .c  om*/
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        Session sessao = HibernateUtil.getSessionFactory().openSession();
        String nome = request.getParameter("nome");
        Professor prof = (Professor) sessao.get(Professor.class, nome);

        prof.setDisciplina(request.getParameter("disciplina"));
        prof.setSobrenome(request.getParameter("sobrenome"));

        Transaction tx = null;
        try {
            tx = sessao.beginTransaction();

            sessao.save(prof);
            sessao.flush();

            tx.commit();
            sessao.close();
            out.println("Professor Atualizado com sucesso.");

        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            out.println("Erro ao atualizar Professor: " + e.getMessage());
        }
    }
}

From source file:incluirEstrela.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w  w w.ja v  a  2 s.  c  o  m*/
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");

    try (PrintWriter out = response.getWriter()) {
        try {
            Estrela e1 = new Estrela();

            e1.setNome(request.getParameter("Nome"));
            e1.setConstelacao(request.getParameter("Constelacao"));
            String distancia = request.getParameter("Distancia");
            if (distancia != null) {
                Integer distanciai = Integer.parseInt(distancia);
                e1.setDistancia_da_Terra(distanciai);
            }

            Session sessao = HibernateUtil.getSessionFactory().openSession();

            Transaction tx = sessao.beginTransaction();

            sessao.save(e1);
            sessao.flush();

            tx.commit();

            sessao.close();

            out.println("Registro inserido com sucesso!");
        } catch (Exception ex) {
            out.println("Erro ao inserir estrela: " + ex.getMessage());
        }

    }
}

From source file:Teste.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* w  w  w. j  ava  2s.com*/
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        Session sessao = HibernateUtil.getSessionFactory().openSession();

        Transaction tx = sessao.beginTransaction();

        Produto prod = new Produto();
        prod.setNome("Batata");
        prod.setDescricao("Vegetal de origem europeia");
        prod.setPreco(1);

        sessao.save(prod);
        sessao.flush();
        tx.commit();
        sessao.close();

    }
}

From source file:SalvarOcorrencia.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w  w  w .  j  a  v a  2s. c  o  m
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        Ocorrencia o1 = new Ocorrencia();
        o1.setNome("Mariana");
        o1.setEndereco("C.A.S");
        o1.setTipo_ocorrencia("Transito");
        o1.setId(01);

        Session sessao = HibernateUtil.getSessionFactory().openSession();

        Transaction tx = sessao.beginTransaction();

        sessao.save(o1);
        sessao.flush();

        tx.commit();

        sessao.close();

        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet SalvarOcorrencia</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet SalvarOcorrencia at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");

    }
}

From source file:SalvarIngrediente.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// ww  w.ja  v  a  2  s . co m
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        try {
            Ingrediente ing = new Ingrediente();
            ing.setNome(request.getParameter("nome"));

            String Q = request.getParameter("qtds");
            if (Q != null) {
                Integer qtds = Integer.parseInt(Q);
                ing.setQtd(qtds);
            }

            String V = request.getParameter("valor");
            if (V != null) {
                Double valor = Double.parseDouble(V);
                ing.setValor(valor);
            }

            Session sessao = HibernateUtil.getSessionFactory().openSession();

            Transaction tx = sessao.beginTransaction();

            sessao.save(ing);
            sessao.flush();

            tx.commit();

            sessao.close();

            out.println("Registro inserido com sucesso!");
        } catch (Exception ex) {
            out.println("Erro ao Gravar: " + ex.getMessage());
        }

    }
}

From source file:criaProduto.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w  ww  . j  a  v a 2 s  . c o m
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        Produto prod = new Produto();
        prod.setNome("Produto_" + new Random().nextLong());
        prod.setFabricante("Pirelli");
        prod.setPreco(400);

        Transaction tx = null;
        try {
            Session sessao = HibernateUtil.getSessionFactory().openSession();

            tx = sessao.beginTransaction();

            sessao.save(prod);
            sessao.flush();

            tx.commit();
            sessao.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
        }

        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet criaProduto</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet criaProduto at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:SalvarPincel.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   ww w  . j a  v a2  s  . co  m
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        Pincel p1 = new Pincel();
        p1.setCor("azul");
        p1.setFabricante("Pilot");
        p1.setNum_serie(10254);

        Session sessao = HibernateUtil.getSessionFactory().openSession();

        Transaction tx = sessao.beginTransaction();
        try {
            sessao.save(p1);
            sessao.flush();
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        }
        sessao.close();

        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet SalvarPincel</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet SalvarPincel at " + request.getContextPath() + "</h1>");
        out.println("<h1>kkkkk " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}