Example usage for org.hibernate Session save

List of usage examples for org.hibernate Session save

Introduction

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

Prototype

Serializable save(Object object);

Source Link

Document

Persist the given transient instance, first assigning a generated identifier.

Usage

From source file:criarEquipe.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w  w w . ja v  a2 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()) {

        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./*w  w  w .  ja  va2s. 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.// www. ja va2 s  .  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();
        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:SaveComplexTypeTest.java

public static void main(String[] args) {
    Session ses = utils.HibernateUtils.getSessionFactory().getCurrentSession();
    Staff s = new Staff();
    ArrayList<Role> role = new ArrayList<>();
    Role r1 = new Role();
    r1.setRoleType(1);/*from ww w  .  j a  va2  s.c  o m*/
    r1.setStaffId(s);

    Role r2 = new Role();
    r2.setRoleType(0);
    r2.setStaffId(s);
    role.add(r1);
    role.add(r2);
    s.setUserName("godmode");
    s.setPassword("123456");
    s.setRoleCollection(role);
    try {
        ses.beginTransaction();
        ses.save(s);
        ses.getTransaction().commit();
        System.out.println("done");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("false");
        ses.getTransaction().rollback();
    }
    System.exit(0);
}

From source file:addProject.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   ww  w .  j  ava  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");
    PrintWriter out = response.getWriter();
    String path = "";
    System.out.println("Welcome to addProject.java");
    try {
        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();

        String company_name = request.getParameter("company_name");
        String project_name = request.getParameter("project_name");
        String mine_location = request.getParameter("mine_location");
        String mine_area = request.getParameter("mine_area");

        int selectedME = Integer.parseInt(request.getParameter("userIdTo").toString());
        System.out.println(selectedME);
        int points = Integer.parseInt(request.getParameter("points"));
        int tasks = Integer.parseInt(request.getParameter("tasks"));
        int userId = (int) hs.getAttribute("userId");
        System.out.println("Got a few the parameters.");
        LoginInfo userIdFrom = new LoginInfo();
        userIdFrom.setUserId(userId);
        //==============due date from table ==================
        String dayfield = request.getParameter("dayfield");
        String monthfield = request.getParameter("monthfield");
        String yearfield = request.getParameter("yearfield");
        String dt = yearfield + "-" + monthfield + "-" + dayfield;
        System.out.println("Date Before Save ::::::" + dt);

        ProjectDetails proj_det = new ProjectDetails();
        proj_det.setUserIdFrom(userIdFrom);
        proj_det.setCompanyName(company_name);
        proj_det.setProjectName(project_name);
        proj_det.setMineLocation(mine_location);
        proj_det.setMineArea(mine_area);
        proj_det.setPoints(points);
        proj_det.setTasks(tasks);
        proj_det.setTasksRemaining(tasks);
        proj_det.setDueDate(dt);
        ss.save(proj_det);
        System.out.println("proj_det saved.");

        LoginInfo userIdTo = new LoginInfo(selectedME);
        ProjectUserDetails pud = new ProjectUserDetails();
        pud.setMineId(proj_det);
        pud.setUserIdTo(userIdTo);
        ss.save(pud);
        System.out.println("pud saved.");

        //           ======logic for user id to==========
        tr.commit();
        //            RequestDispatcher rd = request.getRequestDispatcher("/Geologist/SuccessGeo.jsp");
        //            rd.forward(request, response);

        path = "SuccessGeo.jsp";
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);

    } catch (Exception e) {
        path = "FailureGeo.jsp";
        System.out.println(e.getMessage());
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);
    }

}

From source file:incluirEstrela.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w  w  w . j  av a 2s.  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()) {
        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:regUser.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//www. j av 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");
    PrintWriter out = response.getWriter();
    try {

        Session session = NewHibernateUtil.getSessionFactory().openSession();
        Transaction tr = session.beginTransaction();

        Login lt = new Login();
        lt.setUname(request.getParameter("uname"));
        lt.setPwd(request.getParameter("pwd"));
        session.save(lt);

        UserReg ur = new UserReg();
        ur.setEmail(request.getParameter("email"));
        ur.setMobile(request.getParameter("mobile"));
        ur.setState(request.getParameter("state"));
        ur.setCity(request.getParameter("city"));
        ur.setUid(lt);
        session.save(ur);

        tr.commit();
        RequestDispatcher rd = request.getRequestDispatcher("userhome.jsp");
        rd.forward(request, response);
    }

    catch (Exception ex) {
        out.println(ex.getMessage());
    }
}

From source file:Teste.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w  w w  . ja va 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()) {

        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 www.j a 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()) {

        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:CatalogueAdder.java

private void addCity(City country) {

    Session sess = null;
    try {//w  w w. java  2s.  c o  m
        sess = NewHibernateUtil.currentSession();
        Transaction tx = sess.beginTransaction();
        sess.save(country);
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}