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

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

        Professor prof = new Professor();

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

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

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

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

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

From source file:addGeologist.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// w w w  .  j  ava 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");
    PrintWriter out = response.getWriter();
    String path = "";
    try {
        /* TODO output your page here. You may use following sample code. */
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String user_first_name = request.getParameter("user_first_name");
        String user_last_name = request.getParameter("user_last_name");
        String user_contact_number = request.getParameter("user_contact_number");

        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();

        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(username);
        loginInfo.setPassword(password);
        loginInfo.setRole("G");

        ss.save(loginInfo);

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(loginInfo);
        userInfo.setUserFirstName(user_first_name);
        userInfo.setUserLastName(user_last_name);
        userInfo.setUserContactNumber(user_contact_number);

        ss.save(userInfo);
        HttpSession hs = request.getSession();
        tr.commit();

        path = "SuccessGeo.jsp";
        //            spath = uri.replace("Mineriafinal/addMiningEngineer", "/Geologist/SuccessGeo.jsp");
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);

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

From source file:IncluirPeca.java

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

        String nome = request.getParameter("nome");
        String km = request.getParameter("km");
        String preco = request.getParameter("preco");

        out.println("Incluindo.... <br/>");
        out.println("nome" + nome + "<br/>");
        out.println("Quilometragem Recomendada" + km + "<br/>");
        out.println("Preo" + preco + "<br/>");

        Peca peca = new Peca();
        peca.setNome(nome);
        peca.setKm(km);
        peca.setPreco(preco);

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

        Transaction transacao = sessao.beginTransaction();

        sessao.save(peca);
        sessao.flush();

        transacao.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 IncluirPeca</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet IncluirPeca at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:IncluirProduto.java

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

        Produto p = new Produto();
        p.setDescricao("feijao preto");
        p.setNome("feijao");
        p.setPreco(10.50f);

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

        Transaction transacao = sessao.beginTransaction();

        sessao.save(p);
        sessao.flush();

        transacao.commit();

        sessao.close();

    }
}

From source file:IncluirCliente.java

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

        String nome = request.getParameter("nome");
        String endereco = request.getParameter("endereco");
        String cpf = request.getParameter("cpf");

        out.println("Incluindo...<br/>");
        out.println("nome:" + nome + "<br/>");
        out.println("endereco:" + endereco + "<br/>");
        out.println("cpf:" + cpf + "<br/>");

        Cliente cliente = new Cliente();
        cliente.setCpf(cpf);
        cliente.setEndereco(endereco);
        cliente.setNome(nome);

        //Criar a sesso
        Session sessao = HibernateUtil.getSessionFactory().openSession();

        //Criar a transao
        Transaction t = sessao.beginTransaction();

        //Falar que quer salvar
        sessao.save(cliente);

        //Mandar salvar
        sessao.flush();

        //Commitar a transao
        t.commit();

        //Fechar a sessao
        sessao.close();
    }
}

From source file:DbConnectionJUnitTest.java

@Test
public void saveStudentToExistingTableTest() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;//w  w  w  .j  av a2 s.  com
    Integer id = null;

    try {
        tx = session.beginTransaction();

        Student new_student = new Student("Andrzej", "Wajda", "Mietczyska 34 00-242 Krk");
        id = (Integer) session.save(new_student);

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        System.out.println(e);
        e.printStackTrace();
    } finally {

        session.close();
    }

}

From source file:DbConnectionJUnitTest.java

@Test(expected = MappingException.class)
public void TestSaveException01() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;//from  ww  w  .j a  va2  s  .  co  m

    tx = session.beginTransaction();

    String badinput = new String("bad input");
    session.save(badinput);

    tx.commit();

    session.close();
}

From source file:DbConnectionJUnitTest.java

@Test(expected = IllegalArgumentException.class)
public void TestSaveException02() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;//from   w  w  w.  j a v  a  2  s.  com

    tx = session.beginTransaction();

    String badinput = null;
    session.save(badinput);

    tx.commit();

    session.close();
}

From source file:DbConnectionJUnitTest.java

@Test
public void usuwanieStudenta() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;/*from www. j  ava  2s.c om*/
    Integer idTemporaryStudent = null;

    /***********************/
    /*dodaj studenta do db*/
    /**********************/
    tx = session.beginTransaction();

    Student temporaryStudent = new Student("Karol", "Marzyciel", "Malinowa 23");
    idTemporaryStudent = (Integer) session.save(temporaryStudent);

    tx.commit();
    session.close();

    /****************************/
    /*usu nowododanego studenta*/
    /****************************/
    session = sessionFctry.openSession();
    tx = session.beginTransaction();
    try {
        Student usuwany = (Student) session.get(Student.class, idTemporaryStudent);
        session.delete(usuwany);

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {

        session.close();
    }

    /******************************/
    /*sprbuj pobra tego studenta*/
    /******************************/
    session = sessionFctry.openSession();
    tx = session.beginTransaction();

    Student poszukiwany = (Student) session.get(Student.class, idTemporaryStudent); //to powinno wyrzuci null

    assertNull(poszukiwany);

    tx.commit();
    session.close();

}

From source file:DbConnectionJUnitTest.java

@Test(expected = IllegalArgumentException.class)
public void TestDeleteException01() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;/*  w  w  w.j  a va2s . c o  m*/
    Integer idTemporaryStudent = null;

    /***********************/
    /*dodaj studenta do db*/
    /**********************/
    tx = session.beginTransaction();

    Student temporaryStudent = new Student("Karol", "Marzyciel", "Malinowa 23");
    idTemporaryStudent = (Integer) session.save(temporaryStudent);

    tx.commit();
    session.close();

    /****************************/
    /*usu nowododanego studenta*/
    /****************************/
    session = sessionFctry.openSession();
    tx = session.beginTransaction();

    Student usuwany = (Student) session.get(Student.class, idTemporaryStudent);
    session.delete(null);

    tx.commit();

    session.close();

    /******************************/
    /*sprbuj pobra tego studenta*/
    /******************************/
    session = sessionFctry.openSession();
    tx = session.beginTransaction();

    Student poszukiwany = (Student) session.get(Student.class, idTemporaryStudent); //to powinno wyrzuci null

    assertNull(poszukiwany);

    tx.commit();
    session.close();

}