Example usage for org.hibernate SessionFactory openSession

List of usage examples for org.hibernate SessionFactory openSession

Introduction

In this page you can find the example usage for org.hibernate SessionFactory openSession.

Prototype

Session openSession() throws HibernateException;

Source Link

Document

Open a Session .

Usage

From source file:take_doc_app.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()) {
        /* 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) {
            if (request.getParameter("docid") != null) {
                Patient p = (Patient) hs.getAttribute("patient");
                int docid = Integer.parseInt(request.getParameter("docid"));

                Doctor d1 = new Doctor(docid);
                DoctorAppointment dappo = new DoctorAppointment();
                dappo.setPId(p);
                dappo.setDId(d1);
                dappo.setStatus("PENDING");
                ss.save(dappo);
                request.setAttribute("msg", "Your Appointment has been booked..!");
            }
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("consult_cardiac.jsp");
            rd.forward(request, response);

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

From source file:addnewtest.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w  ww .  j a  v  a2s .  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()) {
        /* 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("lab") != null) {
            Lab lb = (Lab) hs.getAttribute("lab");

            if (request.getParameter("price") != null && request.getParameter("tid") != null) {
                int tid = Integer.parseInt(request.getParameter("tid"));
                Double price = Double.parseDouble(request.getParameter("price"));
                Labtest lt = new Labtest();
                lt.setLId(lb);
                lt.setTId(new Test(tid));
                lt.setTestFees(price);
                ss.save(lt);
                request.setAttribute("msg", "Test added to your test list.!");
            }

            Criteria cr = ss.createCriteria(Test.class);
            ArrayList<Test> alltest = (ArrayList<Test>) cr.list();
            ArrayList<Test> alltest1 = alltest;
            if (alltest.size() > 0) {
                Criteria cr1 = ss.createCriteria(Labtest.class);
                cr1.add(Restrictions.eq("lId", lb));
                ArrayList<Labtest> ltest = (ArrayList<Labtest>) cr1.list();
                if (ltest.size() > 0) {
                    for (int i = 0; i < ltest.size(); i++) {
                        for (int j = 0; j < alltest.size(); j++) {
                            if (ltest.get(i).getTId().equals(alltest.get(j))) {
                                alltest1.remove(j);
                            }
                        }
                    }
                }
                request.setAttribute("alltest", alltest1);
            }
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("lab_addnewtest.jsp");
            rd.forward(request, response);

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

    }
}

From source file:ProceedToPayServ.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        int id = Integer.parseInt(request.getParameter("id"));

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();

        ProductTable product = (ProductTable) ss.get(ProductTable.class, id);

        //            Criteria cr = ss.createCriteria(ProductTable.class);
        //            
        //            ArrayList<ProductTable> pList = (ArrayList<ProductTable>) cr.list();

        request.setAttribute("product", product);
        RequestDispatcher rd = request.getRequestDispatcher("ProceedToPay.jsp");
        rd.forward(request, response);//  w w  w. java2s .  co  m
    }
}

From source file:GetStateServ.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w w w. ja 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 {
        /* TODO output your page here. You may use following sample code. */
        String id = request.getParameter("id");
        String path = "";
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session se = sf.openSession();
        Transaction tr = se.beginTransaction();
        //String sid = request.getParameter("state");
        Criteria cr = se.createCriteria(State.class);
        ArrayList<State> cmli = (ArrayList<State>) cr.list();
        request.setAttribute("sli", cmli);
        if (id.equals("0")) {
            path = "patient_reg.jsp";
        } else if (id.equals("1")) {
            path = "laboratory_reg.jsp";
        } else if (id.equals("2")) {
            path = "doctor_reg.jsp";
        } else {
            path = "whoru.jsp";
        }
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);
        tr.commit();
        out.println("</html>");
    } catch (Exception e) {
        out.println(e.getMessage());
    }
}

From source file:LogServ.java

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

        String path = "";

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

        Criteria cr = ss.createCriteria(Login.class);
        cr.add(Restrictions.eq("username", request.getParameter("username")));
        cr.add(Restrictions.eq("password", request.getParameter("password")));

        ArrayList<Login> all = (ArrayList<Login>) cr.list();
        Login l = all.get(0);
        String role = l.getRole();

        HttpSession hs = request.getSession();
        hs.setAttribute("l2", l);

        tr.commit();
        //                Login l1 = (Login)hs.getAttribute("l2");

        if (all.size() == 0) {

            path = "login.jsp";
            out.println("failed");

        } else {
            if (role.equals("patient")) {

                path = "patienthomepage.jsp";

            } else if (role.equals("doctor")) {

                Criteria cr1 = ss.createCriteria(Doctor.class);
                cr1.add(Restrictions.eq("userId", l));
                ArrayList<Doctor> dList = (ArrayList<Doctor>) cr1.list();
                Doctor d = dList.get(0);
                hs.setAttribute("doctor", d);
                path = "doctorhomepage.jsp";

            } else {
                Criteria cr1 = ss.createCriteria(Lab.class);
                cr1.add(Restrictions.eq("userId", l));
                ArrayList<Lab> lList = (ArrayList<Lab>) cr1.list();
                Lab L = lList.get(0);
                hs.setAttribute("lab", L);
                path = "Labhomepage.jsp";

            }

        }
        // out.println(role);
        // out.print(path);

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

    }

    catch (HibernateException ex) {
        out.println(ex.getMessage());

    }

    finally {
        out.close();
    }
}

From source file:GetAgentDetailServ.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        SessionFactory sf = util.NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();

        int aid = Integer.parseInt(request.getParameter("aid"));

        Criteria cr = ss.createCriteria(AgentDetail.class);
        cr.add(Restrictions.eq("aId", aid));
        AgentDetail ad = (AgentDetail) cr.uniqueResult();

        Criteria cr1 = ss.createCriteria(AgentFeedback.class);
        cr1.add(Restrictions.eq("aId", ad));
        ArrayList<AgentFeedback> afl = (ArrayList<AgentFeedback>) cr1.list();

        request.setAttribute("ad", ad);

        if (!afl.isEmpty()) {
            request.setAttribute("afl", afl);
        }// w  w w.ja v  a 2s  .c  o m

        System.out.println("the aid is " + ad.getAId());
        System.out.println("the uid is " + ad.getUId().getUId());

        Criteria cr2 = ss.createCriteria(PropDetail.class);
        cr2.add(Restrictions.eq("uId", ad.getUId()));
        ArrayList<PropDetail> pdl = (ArrayList<PropDetail>) cr2.list();

        if (!pdl.isEmpty()) {
            request.setAttribute("pdl", pdl);
        }
        System.out.println("the size of proplist is " + pdl.size());

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

    }
}

From source file:ViewPropertyServ.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {/*from www  . ja  va  2 s .  co  m*/
        SessionFactory sf = util.NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();

        Criteria cr = ss.createCriteria(PropFeedback.class);
        cr.addOrder(Order.desc("pfId"));
        List<PropFeedback> pfL = cr.list();

        request.setAttribute("pfL", pfL);

    } catch (HibernateException e) {
        out.println(e.getMessage());
    }
}

From source file:chart_demo.java

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

        //            this file sends records to the chart output page

        List<AssetRecords> a = new ArrayList<>();
        SessionFactory sf = util.NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        int pid = 10;

        String s = "SELECT sum(Price),Year FROM the_asset_consultancy.AssetRecords where Assetid=" + pid
                + " group by Price,Year ;";

        SQLQuery query = ss.createSQLQuery(s);

        List<Object[]> lt = query.list();

        for (Object[] op : lt) {
            AssetRecords ar = new AssetRecords();
            ar.setPrice(op[0].toString());
            ar.setYear(op[1].toString());
            a.add(ar);

            out.println(a);

        }

        System.out.println("Size of array:" + a.size());
        request.setAttribute("al", a);
        RequestDispatcher rd = request.getRequestDispatcher("chart_demo_2.jsp");
        rd.forward(request, response);

    } catch (HibernateException he) {
        out.print(he.getMessage());
    }
}

From source file:creategroup_serv.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {/*from w w  w. j a  v a 2s . c  o m*/

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

        HttpSession hs = request.getSession();
        Login lg2 = (Login) hs.getAttribute("user");
        ArrayList<Login> gpList = (ArrayList<Login>) hs.getAttribute("gpList");
        gpList.add(lg2);
        GroupDetail gd = new GroupDetail();

        gd.setGName(request.getParameter("gname"));
        gd.setUId(lg2);
        gd.setUName(lg2.getUName());

        ss.save(gd);

        for (Login l : gpList) {
            GmemberDetail gmd = new GmemberDetail();
            gmd.setGId(gd);
            gmd.setUId(l);
            ss.save(gmd);
        }
        tr.commit();

        hs.removeAttribute("gpList");
        out.println("Group Created");
        RequestDispatcher rd = request.getRequestDispatcher("getgroups?id=0");
        rd.forward(request, response);

    } catch (HibernateException he) {
        out.print(he.getMessage());
    }
}

From source file:addMiningEngineer.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w w w .  jav  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();
    HttpServletRequest request1 = (HttpServletRequest) request;
    String uri = request1.getRequestURI();

    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("M");

        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);
        userInfo.setUserAddedBy((Integer) request.getSession().getAttribute("userId"));

        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);
    }
}