List of usage examples for org.hibernate Session flush
void flush() throws HibernateException;
From source file:HibernateCoursework.java
public Employee findEmployeeNumber(int empNumber) { Session hibernate = sessionFactory.openSession(); hibernate.beginTransaction();//w w w .j a v a 2s. c om System.out.println("***Session started***"); List tmpList = hibernate.createQuery("from Employee e where e.empNo =" + empNumber).list(); Employee emp = null; for (Iterator iter = tmpList.iterator(); iter.hasNext();) { emp = (Employee) iter.next(); System.out.println("Employee found" + emp.getName().toUpperCase() + "(" + emp.getEmpNo() + ")"); hibernate.getTransaction().commit(); hibernate.flush(); hibernate.close(); System.out.println("***Session closed***"); } return emp; }
From source file:Home.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response);//w w w. j a va 2s . co m System.out.println("run"); //configureer connectie Configuration cfg = new Configuration(); cfg.configure("Hibernate.cfg.xml"); SessionFactory sf = cfg.buildSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); //maak een employee Scoutslid scouts = new Scoutslid(); scouts.setNaam(request.getParameter("Naam")); scouts.setScoutsnaam(request.getParameter("Scoutsnaam")); scouts.setEmailadres(request.getParameter("Emailadres")); //bereid voor om weg te schrijven naar db s.save(scouts); s.flush(); //schrijf weg tx.commit(); //sluit connectie s.close(); //haal de employees op //open een sessie s = sf.openSession(); //Haal alle scoutsleden op List scoutsleden = s.createCriteria(Scoutslid.class).list(); ArrayList<Scoutslid> aScout = new ArrayList(); for (Iterator it1 = scoutsleden.iterator(); it1.hasNext();) { Scoutslid Sc = (Scoutslid) it1.next(); aScout.add(Sc); } request.getSession().setAttribute("Scoutslid", aScout); //sluit de connectie s.close(); request.getRequestDispatcher("results.jsp").forward(request, response); }
From source file:cadastrarProfessor.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*www .j ava2 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()) { 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:IncluirPeca.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./* w w w . j a va 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()) { 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 w ww. j a va 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()) { 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 ava 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 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:SalvarVinho.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//w w w.ja va2 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()) { /* TODO output your page here. You may use following sample code. */ try { Vinho vin = new Vinho(); vin.setNome(request.getParameter("nome")); vin.setUva(request.getParameter("uva")); String v = request.getParameter("ano"); if (v != null) { Integer ano = Integer.parseInt(v); vin.setAno_safra(ano); } Session sessao = HibernateUtil.getSessionFactory().openSession(); Transaction tx = sessao.beginTransaction(); sessao.save(vin); sessao.flush(); tx.commit(); sessao.close(); out.println("Registro gravado com sucesso"); } catch (Exception e) { out.println("Erro ao gravar: " + e.getMessage()); } } }
From source file:academy.util.HibernateUtilTest.java
public Long addToDatabase(Object object) { final Session session = util.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); Long id = null;//from www. j a va 2 s .c om try { id = (Long) session.save(object); } catch (HibernateException e) { transaction.rollback(); } finally { session.flush(); transaction.commit(); session.close(); } return id; }
From source file:acceptance.hibernate.HibernateReferenceTest.java
License:Open Source License
public void testObjectGraphWithReferences() { final Division memory = setupNonpersistentDivision(); final Division persisted = setupPersistentDivision(); final String expectedXml = xstream.toXML(memory); final String persistedXml = xstream.toXML(persisted); final Session session = getSessionFactory().getCurrentSession(); session.beginTransaction();//from w w w . j a va 2 s. c om final Division loaded = (Division) session.createQuery("from Division").uniqueResult(); final String loadedXml = xstream.toXML(loaded); session.flush(); session.getTransaction().commit(); assertEquals(expectedXml, persistedXml); assertEquals(expectedXml, loadedXml); }
From source file:acceptance.hibernate.HibernateReferenceTest.java
License:Open Source License
public void testLazyProxyWithReferences() { setupPersistentDivision();//w ww .j a va 2s.co m final Session session = getSessionFactory().getCurrentSession(); session.beginTransaction(); final Division loaded = (Division) session.createQuery("from Division").uniqueResult(); final Department dept = (Department) loaded.getDepartments().iterator().next(); final Person person = (Person) dept.getPeople().iterator().next(); final Site site = person.getSite(); assertTrue(HibernateProxy.class.isAssignableFrom(site.getClass())); final String loadedXml = xstream.toXML(site); session.flush(); session.getTransaction().commit(); final String expectedXml = "" + "<site>\n" + " <name>Site1</name>\n" + " <people>\n" + " <person>\n" + " <name>Tom</name>\n" + " <department>\n" + " <name>Dep1</name>\n" + " <division>\n" + " <name>Div1</name>\n" + " <departments>\n" + " <department reference=\"../../..\"/>\n" + " </departments>\n" + " </division>\n" + " <people>\n" + " <person reference=\"../../..\"/>\n" + " </people>\n" + " </department>\n" + " <site reference=\"../../..\"/>\n" + " </person>\n" + " </people>\n" + "</site>"; assertEquals(expectedXml, loadedXml); }