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

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

        Cliente p1 = new Cliente();
        p1.setNome("Leandro");
        p1.setIdade(26);
        p1.setEnd("Rua 17");
        p1.setRa(21551055);

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

From source file:HibernateCoursework.java

public void saveEmployee(Employee e) {
    System.out.println("Save Employee");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();//from  ww  w  . j a  v a2s .  co  m
    System.out.println("****************");

    hibernate.save(e);

    System.out.println("***Employee objects saved***");

    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed****");
}

From source file:HibernateCoursework.java

public void saveCustomer(Customer c) {

    System.out.println("Save Customer");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();// w  w w .ja v a 2  s .co  m

    System.out.println("****************");

    hibernate.save(c);

    System.out.println("***Customer objects saved***");

    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");

}

From source file:HibernateCoursework.java

public void saveProject(Project p) {
    System.out.println("Save Project");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();/*  ww  w . j a v a2 s . c  o  m*/

    System.out.println("****************");

    hibernate.save(p);

    System.out.println("***Project objects saved***");

    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public void listEmployees() {
    System.out.println("List Employees");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();/*from   w  w w . j a  v  a  2  s . co  m*/
    System.out.println("***************");

    List employeeList = hibernate.createQuery("from Employee").list();
    System.out.println(employeeList.size() + " employee(s) found:");
    for (Iterator iter = employeeList.iterator(); iter.hasNext();) {
        //assign employee
        Employee e = (Employee) iter.next();
        System.out.println(e.getName());
    }
    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public void listCustomers() {
    System.out.println("List Customers");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();/* ww w.j a  va2s. c om*/
    System.out.println("****************");

    List customerList = hibernate.createQuery("from Customer").list();
    System.out.println(customerList.size() + " customer(s) found:");
    for (Iterator iter = customerList.iterator(); iter.hasNext();) {
        //assign employee
        Customer e = (Customer) iter.next();
        System.out.println(e.getName());
    }
    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public void listProjects() {
    System.out.println("List Projects");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();//  w w  w . ja  va2s .c  om
    System.out.println("****************");

    List projectList = hibernate.createQuery("from Project").list();
    System.out.println(projectList.size() + " project(s) found:");
    for (Iterator iter = projectList.iterator(); iter.hasNext();) {
        //assign employee
        Project p = (Project) iter.next();
        System.out.println(p.getTitle());
    }
    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public void findProj(int projId) {
    System.out.println("Specified Project");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();//from  w  w  w . j  a v a 2  s. com
    System.out.println("*****************");

    List projectList = hibernate.createQuery("from Project p where p.projId =  " + projId).list();
    System.out.println(projectList.size() + " project(s) found:");
    for (Iterator iter = projectList.iterator(); iter.hasNext();) {
        //assign project
        Project p = (Project) iter.next();
        System.out.println("ProjectID = " + p.getProjId());
        System.out.println("Project Title = " + p.getTitle());
        System.out.println("Project Days = " + p.getDays());
    }
    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public void updateProj(Project project, int days, Employee employeeToBeRemoved, Employee employeeToBeAdded) {

    System.out.println("Update Project");
    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();/*ww  w. j  ava  2  s.c o  m*/

    System.out.println("*************");

    project.setDays(days);

    List tmpList = project.getEmployees();
    tmpList.remove(employeeToBeRemoved);
    tmpList.add(employeeToBeAdded);

    project.setEmployees(tmpList);

    hibernate.update(project);

    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");

}

From source file:HibernateCoursework.java

public void deleteEmployee(Employee deleteEmployee) {

    System.out.println("Delete Employee");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();/* www  . j  a  v  a 2  s . co  m*/
    System.out.println("***Session started***");

    System.out.println("***Delete employee***");

    hibernate.delete(deleteEmployee);

    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");

}