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

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

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

        String city = "";

        HttpSession hs = request.getSession();
        Login ln = (Login) hs.getAttribute("user");

        System.out.println(ln.getUName() + "is a " + ln.getURole());

        switch (ln.getURole()) {
        case "agent":
            Criteria cr = ss.createCriteria(AgentDetail.class);
            cr.add(Restrictions.eq("uId", ln));
            //                   / cr.add(Restrictions.ne(city, ln))
            ArrayList<AgentDetail> ad = (ArrayList<AgentDetail>) cr.list();
            city = ad.get(0).getACity();
            break;

        case "customer":
            Criteria cr1 = ss.createCriteria(CustomerDetail.class);
            cr1.add(Restrictions.eq("uId", ln));
            ArrayList<CustomerDetail> cd = (ArrayList<CustomerDetail>) cr1.list();
            city = cd.get(0).getCCity();
            break;

        default:
            System.out.println("No city");

        }

        System.out.println(city + " is the city");

        Criteria cr = ss.createCriteria(AgentDetail.class);
        cr.add(Restrictions.eq("aCity", city));
        cr.add(Restrictions.ne("uId", ln));
        ArrayList<AgentDetail> adl = (ArrayList<AgentDetail>) cr.list();

        System.out.println("the size of agent aray is " + adl.size());

        Criteria cr1 = ss.createCriteria(CustomerDetail.class);
        cr1.add(Restrictions.ne("uId", ln));
        cr1.add(Restrictions.eq("cCity", city));

        ArrayList<CustomerDetail> cdl = (ArrayList<CustomerDetail>) cr1.list();

        System.out.println("the size of cust aray is " + cdl.size());

        if (!adl.isEmpty() && !cdl.isEmpty()) {
            request.setAttribute("adl", adl);
            request.setAttribute("cdl", cdl);
            RequestDispatcher rd = request.getRequestDispatcher("creategroup.jsp");
            rd.forward(request, response);
        } else if (!adl.isEmpty()) {
            request.setAttribute("adl", adl);
            RequestDispatcher rd = request.getRequestDispatcher("creategroup.jsp");
            rd.forward(request, response);
        } else if (!cdl.isEmpty()) {
            request.setAttribute("cdl", cdl);
            RequestDispatcher rd = request.getRequestDispatcher("creategroup.jsp");
            rd.forward(request, response);
        } else {
            String msg = "Sorry, there are no users from your city.";
            request.setAttribute("msg", msg);
            RequestDispatcher rd = request.getRequestDispatcher("creategroup.jsp");
            rd.forward(request, response);
        }

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

From source file:logoutserv.java

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

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

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

    finally {
        out.close();
    }
}

From source file:past_systemreport.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// ww  w . ja  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("patient") != null) {
            Patient a = (Patient) hs.getAttribute("patient");
            int pid = a.getPId();
            Calculationmeteredata cal = (Calculationmeteredata) ss.get(Calculationmeteredata.class, pid);
            Criteria cr = ss.createCriteria(Calculationmeteredata.class);
            cr.add(Restrictions.eq("pId", a));
            // cr.add(Restrictions.eq("status", "PENDING"));
            ArrayList<DoctorAppointment> da = (ArrayList<DoctorAppointment>) cr.list();
            if (da.size() > 0) {
                request.setAttribute("da", da);
            }
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("patient_my_system_report.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:addProject.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  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();
    String path = "";
    System.out.println("Welcome to addProject.java");
    try {
        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();

        String company_name = request.getParameter("company_name");
        String project_name = request.getParameter("project_name");
        String mine_location = request.getParameter("mine_location");
        String mine_area = request.getParameter("mine_area");

        int selectedME = Integer.parseInt(request.getParameter("userIdTo").toString());
        System.out.println(selectedME);
        int points = Integer.parseInt(request.getParameter("points"));
        int tasks = Integer.parseInt(request.getParameter("tasks"));
        int userId = (int) hs.getAttribute("userId");
        System.out.println("Got a few the parameters.");
        LoginInfo userIdFrom = new LoginInfo();
        userIdFrom.setUserId(userId);
        //==============due date from table ==================
        String dayfield = request.getParameter("dayfield");
        String monthfield = request.getParameter("monthfield");
        String yearfield = request.getParameter("yearfield");
        String dt = yearfield + "-" + monthfield + "-" + dayfield;
        System.out.println("Date Before Save ::::::" + dt);

        ProjectDetails proj_det = new ProjectDetails();
        proj_det.setUserIdFrom(userIdFrom);
        proj_det.setCompanyName(company_name);
        proj_det.setProjectName(project_name);
        proj_det.setMineLocation(mine_location);
        proj_det.setMineArea(mine_area);
        proj_det.setPoints(points);
        proj_det.setTasks(tasks);
        proj_det.setTasksRemaining(tasks);
        proj_det.setDueDate(dt);
        ss.save(proj_det);
        System.out.println("proj_det saved.");

        LoginInfo userIdTo = new LoginInfo(selectedME);
        ProjectUserDetails pud = new ProjectUserDetails();
        pud.setMineId(proj_det);
        pud.setUserIdTo(userIdTo);
        ss.save(pud);
        System.out.println("pud saved.");

        //           ======logic for user id to==========
        tr.commit();
        //            RequestDispatcher rd = request.getRequestDispatcher("/Geologist/SuccessGeo.jsp");
        //            rd.forward(request, response);

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

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

}

From source file:take_test.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w  w  w. 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 ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();
        if (hs.getAttribute("patient") != null) {
            if (request.getParameter("labid") != null) {
                Patient p = (Patient) hs.getAttribute("patient");
                int labid = Integer.parseInt(request.getParameter("labid"));
                Lab la = new Lab(labid);

                Criteria cr = ss.createCriteria(Labtest.class);
                cr.add(Restrictions.eq("lId", la));
                ArrayList<Labtest> ltlist = (ArrayList<Labtest>) cr.list();
                if (ltlist.size() > 0) {
                    request.setAttribute("ltlist", ltlist);
                }
                tr.commit();
                RequestDispatcher rd = request.getRequestDispatcher("patient_labtest_takeappointment.jsp");
                rd.forward(request, response);
            } else {
                tr.commit();
                RequestDispatcher rd = request.getRequestDispatcher("consult_lab.jsp");
                rd.forward(request, response);
            }
        } else {
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
            rd.forward(request, response);
        }
    } catch (HibernateException he) {
        he.getMessage();
    } finally {
        out.close();
    }
}

From source file:searchagent_byarea.java

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

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

        String stateid = request.getParameter("state");
        String cityid = request.getParameter("city");
        String areaid = request.getParameter("area");
        String aname = request.getParameter("aname");

        String state = "";
        Criteria cr = ss.createCriteria(StateMaster.class);
        cr.add(Restrictions.eq("sId", Integer.parseInt(stateid)));
        ArrayList<StateMaster> ar = (ArrayList<StateMaster>) cr.list();
        System.out.println("----------" + ar.size());
        if (ar.isEmpty()) {

        } else {
            state = ar.get(0).getSName();
            System.out.println("-------" + state);
        }

        String city = "";
        Criteria cr2 = ss.createCriteria(CityMaster.class);
        cr2.add(Restrictions.eq("cityId", Integer.parseInt(cityid)));
        ArrayList<CityMaster> ar2 = (ArrayList<CityMaster>) cr2.list();
        System.out.println("----------" + ar2.size());
        if (ar2.isEmpty()) {

        } else {
            city = ar2.get(0).getCityName();
            System.out.println("-------" + city);
        }

        String area = "";
        Criteria cr3 = ss.createCriteria(AreaMaster.class);
        cr3.add(Restrictions.eq("areaId", Integer.parseInt(areaid)));
        ArrayList<AreaMaster> ar3 = (ArrayList<AreaMaster>) cr3.list();
        System.out.println("----------" + ar3.size());
        if (ar3.isEmpty()) {

        } else {
            area = ar3.get(0).getAreaName();
            System.out.println("-------" + area);
        }

        Criteria cr4 = ss.createCriteria(AgentDetail.class);
        cr4.add(Restrictions.eq("aState", state));
        cr4.add(Restrictions.eq("aCity", city));
        cr4.add(Restrictions.eq("aArea", area));

        ArrayList<AgentDetail> ar4 = (ArrayList<AgentDetail>) cr4.list();
        System.out.println("-------AgentList Size---" + ar4.size());
        if (ar4.isEmpty()) {
            out.print("no such agent");
            String msg = "Sorry, No results found.";
            request.setAttribute("error", msg);
        } else {
            request.setAttribute("agent_array", ar4);

        }

        RequestDispatcher rd = request.getRequestDispatcher("getstate?id=4");
        rd.forward(request, response);
        tr.commit();

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

From source file:login_serv.java

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

        //            Login li=new Login();

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

        Criteria cr = ss.createCriteria(Login.class);

        String uname = request.getParameter("username");
        String pass = request.getParameter("psswd");

        cr.add(Restrictions.eq("uName", uname));
        cr.add(Restrictions.eq("pwd", pass));

        ArrayList<Login> alli = (ArrayList<Login>) cr.list();
        //            out.println("jgfghfgh");
        if (alli.isEmpty()) {

            String msg = "Invalid Username or Password.";
            request.setAttribute("error", msg);
            RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");

            rd.forward(request, response);
        }

        else {
            Login l = alli.get(0);

            HttpSession session = request.getSession();
            session.setAttribute("user", l);

            //request.setAttribute("usname",uname);
            switch (l.getURole()) {
            case "customer": {
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                rd.include(request, response);
                break;
            }
            case "agent": {

                Criteria cr1 = ss.createCriteria(AgentDetail.class);
                cr1.add(Restrictions.eq("uId", l));
                ArrayList<AgentDetail> adl = (ArrayList<AgentDetail>) cr1.list();
                AgentDetail ad = adl.get(0);

                if (ad.getAStatus().equals("Accepted")) {
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                    rd.include(request, response);
                }

                else if (ad.getAStatus().equals("Pending")) {
                    String msg = "Your details are still being verified. Please try later.";
                    request.setAttribute("error", msg);
                    RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");

                    rd.forward(request, response);
                }
                break;
            }
            case "admin": {
                RequestDispatcher rd = request.getRequestDispatcher("adminhome.jsp");
                rd.include(request, response);
                break;
            }
            default: {
                RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                rd.include(request, response);
                break;
            }

            }

        }

        //            li=alli.get(0);
        tr.commit();

    } catch (Exception e) {
        System.out.print(e.getMessage());
    }

}

From source file:getarea.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w  w  w . j ava  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 cityid = Integer.parseInt(request.getParameter("city"));
        //out.print(cityid);

        CityMaster cm = new CityMaster();
        cm.setCityId(cityid);

        Criteria cr = ss.createCriteria(AreaMaster.class);
        cr.add(Restrictions.eq("cityId", cm));
        // cr.add(Restrictions.eq("sId",sm));

        ArrayList<AreaMaster> ctlist = (ArrayList<AreaMaster>) cr.list();

        for (int i = 0; i < ctlist.size(); i++) {
            AreaMaster am = ctlist.get(i);
            String arname = am.getAreaName();
            System.out.println("-------------getarea" + arname);
            System.out.println("------------- " + am.getAreaId());
            out.print("<option value=" + am.getAreaId() + ">" + arname + "</option>");
        }

        tr.commit();

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

From source file:getgroups.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// www .  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();
    HttpSession hs = request.getSession();
    try {

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

        Login ln = (Login) hs.getAttribute("user");
        System.out.println(ln.getUName() + "is the user");
        int id = Integer.parseInt(request.getParameter("id"));

        Criteria cr = ss.createCriteria(GmemberDetail.class);
        cr.add(Restrictions.eq("uId", ln));
        ArrayList<GmemberDetail> gmdl = (ArrayList<GmemberDetail>) cr.list();
        //            ArrayList<GroupDetail> gdl1 = new ArrayList<>();
        //            for (GmemberDetail g : gmdl) {
        //                
        //                System.out.println(g.getGId());
        //                Criteria cr1 = ss.createCriteria(GroupDetail.class);
        //                cr1.add(Restrictions.eq("gId", g.getGId().getGId()));
        //
        //                GroupDetail gd = (GroupDetail) cr1.uniqueResult();
        //                gdl1.add(gd);
        //
        //            }

        System.out.println("=====glist size=====" + gmdl.size());
        request.setAttribute("grouplist", gmdl);

        if (id == 0) {
            RequestDispatcher rd = request.getRequestDispatcher("group_home.jsp");
            rd.forward(request, response);
        } else if (id == 1)

        {

            int uid = Integer.parseInt(request.getParameter("uid"));
            request.setAttribute("uid", uid);
            RequestDispatcher rd = request.getRequestDispatcher("groupsofuser.jsp");
            rd.forward(request, response);
        }

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

From source file:go_to_user.java

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

        String id = request.getParameter("id");

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

        Login l = (Login) ss.get(Login.class, Integer.parseInt(id));
        System.out.println("uid is " + l.getUId());

        switch (l.getURole()) {
        case "customer": {
            Criteria cr = ss.createCriteria(CustomerDetail.class);
            cr.add(Restrictions.eq("uId", l));
            CustomerDetail cd = (CustomerDetail) cr.uniqueResult();

            request.setAttribute("fname", cd.getCFname());
            request.setAttribute("lname", cd.getCLname());

            request.setAttribute("email", cd.getCEmail());
            request.setAttribute("mobile", cd.getCNum());

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

            break;
        }

        case "agent": {

            Criteria cr = ss.createCriteria(AgentDetail.class);
            cr.add(Restrictions.eq("uId", l));
            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);
            }
            RequestDispatcher rd = request.getRequestDispatcher("agentdescription.jsp");
            rd.forward(request, response);

            break;
        }
        }

    }

    catch (HibernateException he) {
        System.out.println(he.getMessage());
    }

}