List of usage examples for org.hibernate Session get
Object get(String entityName, Serializable id);
From source file:DbConnectionJUnitTest.java
@Test public void usuwanieStudenta() { Session session = sessionFctry.openSession(); Transaction tx = null;//from 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(); 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;// ww w.j ava2 s . 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(); }
From source file:DbConnectionJUnitTest.java
@Test(expected = MappingException.class) public void TestDeleteException02() { Session session = sessionFctry.openSession(); Transaction tx = null;//w ww. j a v a2 s . 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(); String usuwany = new String("asd"); session.delete(usuwany); 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(); }
From source file:admin_add_doctors.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//www .j a 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"); PrintWriter out = response.getWriter(); try { SessionFactory sf = HibernateUtil.getSessionFactory(); Session ss = sf.openSession(); Transaction tr = ss.beginTransaction(); HttpSession hs = request.getSession(); if (hs.getAttribute("admin") != null) { Admin a = (Admin) hs.getAttribute("admin"); if (request.getParameter("status") != null && request.getParameter("dId") != null) { int apid = Integer.parseInt(request.getParameter("dId")); Doctor doc1 = (Doctor) ss.get(Doctor.class, apid); if (request.getParameter("status").equals("approve")) { doc1.setStatus("ACTIVE"); ss.update(doc1); String subject = "Your Appointment is Approved.!"; String content = "Hi, Dr." + doc1.getDFirstname() + " " + doc1.getDLastname() + "\n" + "Your Request has been approved by Admin" + ".\n"; String mail = doc1.getEmailId(); String[] recipients = new String[] { mail }; //String[] bccRecipients = new String[]{"sunilkotadiya777@gmail.com"}; if (new MailUtil().sendMail(recipients, subject, content)) { } request.setAttribute("msg", "Appointment accepted..!"); } else if (request.getParameter("status").equals("reject")) { doc1.setStatus("REJECTED"); ss.update(doc1); String subject = "Your Request is Rejected.!"; String content = "Hi, " + doc1.getDFirstname() + " " + doc1.getDLastname() + "Your Request to join Cardiac Countermeasure has been rejected by admin" + ".\n anil "; String mail = doc1.getEmailId(); String[] recipients = new String[] { mail }; //String[] bccRecipients = new String[]{"sunilkotadiya777@gmail.com"}; if (new MailUtil().sendMail(recipients, subject, content)) { } request.setAttribute("msg", "Appointment rejected..!"); } } Criteria cr = ss.createCriteria(Doctor.class); //cr.add(Restrictions.eq("dId", a)); cr.add(Restrictions.eq("status", "not_active")); ArrayList<Doctor> da = (ArrayList<Doctor>) cr.list(); if (da.size() > 0) { request.setAttribute("da", da); } tr.commit(); RequestDispatcher rd = request.getRequestDispatcher("admin_add_doctors.jsp"); rd.forward(request, response); } else { tr.commit(); RequestDispatcher rd = request.getRequestDispatcher("login.jsp"); rd.forward(request, response); } } catch (HibernateException he) { out.println(he.getMessage()); } finally { out.close(); } }
From source file:systemgeneratedreport.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from ww w.j av 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()) { /* TODO output your page here. You may use following sample code. */ SessionFactory sf = HibernateUtil.getSessionFactory(); Session ss = sf.openSession(); Transaction tr = ss.beginTransaction(); HttpSession hs = request.getSession(); if (hs.getAttribute("patient") != null) { Patient p1 = (Patient) hs.getAttribute("patient"); if (request.getParameter("calcid") != null) { int calcid = Integer.parseInt(request.getParameter("calcid")); Calculationmeteredata cm1 = (Calculationmeteredata) ss.get(Calculationmeteredata.class, calcid); String color = "red"; if (cm1.getCalculationmeteredataId() != null) { String gender = "MALE"; Criteria optcr = ss.createCriteria(PatientfactorAnswer.class); optcr.add(Restrictions.eq("calculationmeteredataId", cm1)); ArrayList<PatientfactorAnswer> anslist = (ArrayList<PatientfactorAnswer>) optcr.list(); if (anslist.size() > 0) { FactorOption agefac = new FactorOption(); for (PatientfactorAnswer opp : anslist) { if (opp.getFactorId().equals(new FactorDetails(1))) { if (opp.getFactorOptionId().getFoptionId().equals(16)) { gender = "FEMALE"; } System.out.println("Gender is :" + gender + opp.getFactorOptionId()); } if (opp.getFactorId().equals(new FactorDetails(2))) { agefac = opp.getFactorOptionId(); System.out.println("Age is :" + agefac.getFactorOption()); } } if (gender.equals("MALE")) { // Set Chart Data String[] chartdata = new String[4]; String[] rchartdata = new String[4]; Criteria cr01 = ss.createCriteria(AbsulateRiskMale.class); cr01.add(Restrictions.eq("foptionid", agefac)); cr01.setProjection(Projections.max("green")); if (cr01.uniqueResult() != null) { chartdata[0] = cr01.uniqueResult().toString(); int cdata = Integer.parseInt(chartdata[0]); if (cdata >= cm1.getMetervalue()) { color = "green"; } } Criteria cr02 = ss.createCriteria(AbsulateRiskMale.class); cr02.add(Restrictions.eq("foptionid", agefac)); cr02.setProjection(Projections.max("violet")); if (cr02.uniqueResult() != null) { chartdata[1] = cr02.uniqueResult().toString(); int cdata = Integer.parseInt(chartdata[1]); if (cdata >= cm1.getMetervalue()) { color = "violet"; } } System.out.println(chartdata[1]); System.out.println(color); Criteria cr03 = ss.createCriteria(AbsulateRiskMale.class); cr03.add(Restrictions.eq("foptionid", agefac)); cr03.setProjection(Projections.max("yellow")); if (cr03.uniqueResult() != null) { chartdata[2] = cr03.uniqueResult().toString(); int cdata = Integer.parseInt(chartdata[2]); if (cdata >= cm1.getMetervalue()) { color = "yellow"; } } chartdata[3] = "54"; // RchartData create Criteria cr010 = ss.createCriteria(AbsulateRiskMale.class); cr010.add(Restrictions.eq("foptionid", agefac)); cr010.setProjection(Projections.max("rGreen")); if (cr010.uniqueResult() != null) { rchartdata[0] = cr010.uniqueResult().toString(); } Criteria cr020 = ss.createCriteria(AbsulateRiskMale.class); cr020.add(Restrictions.eq("foptionid", agefac)); cr020.setProjection(Projections.max("rViolet")); if (cr020.uniqueResult() != null) { rchartdata[1] = cr020.uniqueResult().toString(); } System.out.println(chartdata[1]); System.out.println(color); Criteria cr030 = ss.createCriteria(AbsulateRiskMale.class); cr030.add(Restrictions.eq("foptionid", agefac)); cr030.setProjection(Projections.max("rYellow")); if (cr030.uniqueResult() != null) { rchartdata[2] = cr030.uniqueResult().toString(); } rchartdata[3] = "30"; request.setAttribute("rchartdata", rchartdata); request.setAttribute("chartdata", chartdata); } else if (gender.equals("FEMALE")) { // Set Chart Data String[] chartdata = new String[4]; String[] rchartdata = new String[4]; Criteria cr01 = ss.createCriteria(AbsoluteRiskFemale.class); cr01.add(Restrictions.eq("foptionid", agefac)); cr01.setProjection(Projections.max("green")); if (cr01.uniqueResult() != null) { chartdata[0] = cr01.uniqueResult().toString(); int cdata = Integer.parseInt(chartdata[0]); if (cdata >= cm1.getMetervalue()) { color = "green"; } } Criteria cr02 = ss.createCriteria(AbsoluteRiskFemale.class); cr02.add(Restrictions.eq("foptionid", agefac)); cr02.setProjection(Projections.max("violet")); if (cr02.uniqueResult() != null) { chartdata[1] = cr02.uniqueResult().toString(); int cdata = Integer.parseInt(chartdata[1]); if (cdata >= cm1.getMetervalue()) { color = "violet"; } } Criteria cr03 = ss.createCriteria(AbsoluteRiskFemale.class); cr03.add(Restrictions.eq("foptionid", agefac)); cr03.setProjection(Projections.max("yellow")); if (cr03.uniqueResult() != null) { chartdata[2] = cr03.uniqueResult().toString(); int cdata = Integer.parseInt(chartdata[2]); if (cdata >= cm1.getMetervalue()) { color = "yellow"; } } chartdata[3] = "30"; // RchartData create Criteria cr010 = ss.createCriteria(AbsoluteRiskFemale.class); cr010.add(Restrictions.eq("foptionid", agefac)); cr010.setProjection(Projections.max("rGreen")); if (cr010.uniqueResult() != null) { rchartdata[0] = cr010.uniqueResult().toString(); } Criteria cr020 = ss.createCriteria(AbsoluteRiskFemale.class); cr020.add(Restrictions.eq("foptionid", agefac)); cr020.setProjection(Projections.max("rViolet")); if (cr020.uniqueResult() != null) { rchartdata[1] = cr020.uniqueResult().toString(); } System.out.println(chartdata[1]); System.out.println(color); Criteria cr030 = ss.createCriteria(AbsoluteRiskFemale.class); cr030.add(Restrictions.eq("foptionid", agefac)); cr030.setProjection(Projections.max("rYellow")); if (cr030.uniqueResult() != null) { rchartdata[2] = cr030.uniqueResult().toString(); } rchartdata[3] = "20"; request.setAttribute("rchartdata", rchartdata); request.setAttribute("chartdata", chartdata); } Criteria testcr = ss.createCriteria(TestRecommend.class); testcr.add(Restrictions.eq("color", color)); ArrayList<TestRecommend> tlist = (ArrayList<TestRecommend>) testcr.list(); if (tlist.size() > 0) { request.setAttribute("testlist", tlist); } request.setAttribute("calcdata", cm1); request.setAttribute("anslist", anslist); } } tr.commit(); RequestDispatcher rd = request.getRequestDispatcher("system_generated_report1.jsp"); rd.forward(request, response); } } else { tr.commit(); RequestDispatcher rd = request.getRequestDispatcher("login.jsp"); rd.forward(request, response); } } }
From source file:ProcuraPeca.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w. j av 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()) { Session sessao = HibernateUtil.getSessionFactory().openSession(); String id = request.getParameter("id"); int numero = Integer.parseInt(id); Peca p; p = (Peca) sessao.get(Peca.class, numero); out.println("Pea : " + p.getNome() + "/" + p.getKm() + "/" + p.getPreco()); 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 ProcuraPeca</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet ProcuraPeca at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } }
From source file:agentstatus.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from 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"); PrintWriter out = response.getWriter(); try { SessionFactory sf = NewHibernateUtil.getSessionFactory(); Session ss = sf.openSession(); Transaction tr = ss.beginTransaction(); int id = Integer.parseInt(request.getParameter("id")); int agentid = Integer.parseInt(request.getParameter("aid")); // Touroperator to = (Touroperator)se.get(Touroperator.class, spid); AgentDetail ad = (AgentDetail) ss.get(AgentDetail.class, agentid); if (id == 0) { //Accepted out.print(agentid + "accepted"); AgentDetail ad2 = new AgentDetail(); ad2.setAAddress1(ad.getAAddress1()); ad2.setAAddress2(ad.getAAddress2()); ad2.setAArea(ad.getAArea()); ad2.setACity(ad.getACity()); ad2.setACompanyname(ad.getACompanyname()); ad2.setADescription(ad.getADescription()); ad2.setAEmail(ad.getAEmail()); ad2.setAFname(ad.getAFname()); ad2.setALname(ad.getALname()); ad2.setAId(ad.getAId()); ad2.setAImg(ad.getAImg()); ad2.setANo(ad.getANo()); ad2.setARating(ad.getARating()); ad2.setAState(ad.getAState()); ad2.setUId(ad.getUId()); ad2.setAWorkx(ad.getAWorkx()); ad2.setAStatus("Accepted"); ss.evict(ad); ss.update(ad2); tr.commit(); } else if (id == 1) { //Denied out.print(agentid + "denied"); ss.delete(ad); tr.commit(); } } catch (HibernateException e) { out.println(e.getMessage()); } }
From source file:doc_profile.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww . j a v 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"); PrintWriter out = response.getWriter(); try { SessionFactory sf = HibernateUtil.getSessionFactory(); Session ss1 = sf.openSession(); Transaction tr1 = ss1.beginTransaction(); // tr1.begin(); HttpSession hs = request.getSession(true); if (hs.getAttribute("doctor") != null) { Doctor pi = (Doctor) hs.getAttribute("doctor"); if (request.getParameter("btn_update") != null) { Doctor dd = (Doctor) ss1.get(Doctor.class, pi.getDId()); Addressdetails ad = dd.getAddressid(); String line = request.getParameter("line1"); ad.setLine1(line.trim()); ad.setLine2(request.getParameter("line2").trim()); ad.setPincode(request.getParameter("pincode").trim()); ss1.saveOrUpdate(ad); dd.setDFirstname(request.getParameter("p_firstname").trim()); dd.setDLastname(request.getParameter("p_lastname").trim()); dd.setGender(request.getParameter("gender").trim()); dd.setBirthDate(request.getParameter("birth_date").trim()); dd.setContactnum(request.getParameter("contact_num").trim()); dd.setEmailId(request.getParameter("email_id").trim()); ss1.saveOrUpdate(dd); pi = dd; } tr1.commit(); request.setAttribute("pdata", pi); RequestDispatcher rd = request.getRequestDispatcher("Doctor_myprofile.jsp"); rd.forward(request, response); } else { tr1.commit(); RequestDispatcher rd = request.getRequestDispatcher("login.jsp"); rd.forward(request, response); } } catch (HibernateException he) { out.println(he.getMessage()); } finally { out.close(); } }
From source file:$.DefaultDAO.java
License:Open Source License
/** * Checks whether the entity with provided key exists */*from w w w.j av a2s . co m*/ * @param clazz entity class * @param id key to check * @return true is entity of the provided class with provided key exists; false otherwise */ private <ENTITY extends IdentifiedEntityInterface> boolean exists(Class<ENTITY> clazz, Long id) { if (id == null) { return false; } Session session = getSession(); return session.get(clazz, id) != null; }
From source file:aa.webapp.servlet.hibernate.servlet.GetPersonByID.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./* ww w.j av 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 { int empId = Integer.parseInt(request.getParameter("empId")); logger.info("Request Param empId=" + empId); SessionFactory sessionFactory = (SessionFactory) request.getServletContext().getAttribute("SessionFactory"); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); Person emp = (Person) session.get(Person.class, new Long(empId)); tx.commit(); PrintWriter out = response.getWriter(); response.setContentType("text/html"); if (emp != null) { out.print("<html><body><h2>Employee Details</h2>"); out.print("<table border=\"1\" cellspacing=10 cellpadding=5>"); out.print("<th>Employee ID</th>"); out.print("<th>Employee Name</th>"); out.print("<th>Employee Role</th>"); out.print("<tr>"); out.print("<td>" + empId + "</td>"); out.print("<td>" + emp.getName() + "</td>"); out.print("<td>" + emp.getRole() + "</td>"); out.print("</tr>"); out.print("</table></body><br/>"); out.print("</html>"); } else { out.print("<html><body><h2>No Employee Found with ID=" + empId + "</h2></body></html>"); } }