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

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w w  w  . java  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");
    PrintWriter out = response.getWriter();
    try {
        String path = "refresh_page.jsp";
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();

        Addressdetails ad = new Addressdetails();
        ad.setLine1(request.getParameter("line1"));
        ad.setLine2(request.getParameter("line2"));
        ad.setCity(request.getParameter("city"));
        ad.setState(request.getParameter("state"));
        ad.setPincode(request.getParameter("pincode"));
        ss.save(ad);

        Login lo = new Login();
        lo.setUsername(request.getParameter("user_id"));
        lo.setPassword(request.getParameter("password"));
        lo.setRole("Laboratory");
        ss.save(lo);

        Lab la = new Lab();
        la.setLabName(request.getParameter("lab_name"));
        la.setWebsite(request.getParameter("website"));
        la.setContactNum(request.getParameter("cont_num"));
        la.setAddressid(ad);
        la.setStatus("not Active");
        la.setUserId(lo);
        ss.save(la);

        tr.commit();

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

        out.println("success");

    }

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

}

From source file:propfeedbacklist_serv.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w  ww .  j a  v  a2s. 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 = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();

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

        ArrayList<PropFeedback> pfl = (ArrayList<PropFeedback>) cr.list();

        if (!pfl.isEmpty()) {
            request.setAttribute("pfl", pfl);
            RequestDispatcher rd = request.getRequestDispatcher("propfeedbacklist.jsp");
            rd.forward(request, response);
        }

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

From source file:ServletActualizarTrabajador.java

protected void doPut(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    SessionFactory factory = HibernateUtilidades.getSessionFactory();
    Session sesion = factory.openSession();
    Transaction tranza = sesion.beginTransaction();

    String id = request.getParameter("id");
    String nombre = request.getParameter("nombre");
    String paterno = request.getParameter("paterno");
    String materno = request.getParameter("materno");

    Trabajador t = new Trabajador();
    t.setIdTrabajador(new Integer(id));
    t.setNombre(nombre);// w  w w .  j av  a  2s . c  o m
    t.setPaterno(paterno);
    t.setMaterno(materno);
    sesion.update(t);
    tranza.commit();
    sesion.close();

    out.println("Trabajador Actualizado");
}

From source file:getdoctor.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   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 = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        String code = "";

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

        ArrayList<Doctor> all = (ArrayList<Doctor>) cr.list();

        Criteria cr1 = ss.createCriteria(Doctor.class);
        cr1.add(Restrictions.eq("status", "ACTIVE"));
        if (request.getParameter("sort") != null) {
            String so = request.getParameter("sort");
            if (so.equals("1")) {
                cr1.addOrder(Order.asc("dFirstname"));
            } else if (so.equals("2")) {
                cr1.addOrder(Order.desc("dFirstname"));
            }
            //.add( Restrictions.like("name", "F%")
            //.addOrder( Order.asc("name") )
            //.addOrder( Order.desc("age") )
            //.setMaxResults(50)

            //                cr.add(Restrictions.eq("password", request.getParameter("password")));
        }
        if (request.getParameter("search") != null) {
            String so = request.getParameter("search");
            cr1.add(Restrictions.like("dFirstname", so + "%"));
        }
        ArrayList<Doctor> all1 = (ArrayList<Doctor>) cr1.list();
        if (all1.size() > 0) {
            for (int i = 0; i < all1.size(); i++) {
                Doctor dd = all1.get(i);
                out.println("<li class=\"author-comments\" style=\"margin-top: 36px\">\n"
                        + "                                <div class=\"media\">\n"
                        + "                                    <div class=\"media-left\">    \n"
                        + "                                        <img alt=\"img\" src=\"images/doctor-2.jpg\" class=\"media-object news-img\">      \n"
                        + "                                    </div>\n"
                        + "                                    <div class=\"media-body\">\n"
                        + "                                        <h4 class=\"author-name\">Dr. "
                        + dd.getDFirstname() + " " + dd.getDLastname() + "</h4>\n"
                        + "                                        <span class=\"comments-date\"> "
                        + dd.getQualificationId().getQualificationName() + "</span>\n"
                        + "                                        <p>"
                        + dd.getAddressid().getCityId().getCityName().toString() + "</p>\n"
                        + "                                        <p class=\"fa fa-check-circle\" style=\"color: green\">"
                        + dd.getStatus() + "</p>\n"
                        + "                                        <div class=\"ui heart rating\" data-rating=\"1\" data-max-rating=\"3\"></div>\n"
                        + "                                        <a class=\"reply-btn\" href=\"take_doc_app?docid="
                        + dd.getDId() + "\">Take Appointment</a>\n"
                        + "                                    </div>\n"
                        + "                                </div>\n" + "                            </li>");
            }
        }
        //   request.setAttribute("dlist", all);

    } catch (HibernateException he) {

        he.getMessage();

    } finally {

        out.close();
    }
}

From source file:agent_update.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   w w  w. ja  v 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, FileUploadException {
    response.setContentType("text/html;charset=UTF-8");

    HttpSession hs = request.getSession();
    PrintWriter out = response.getWriter();

    try {

        if (hs.getAttribute("user") != null) {
            Login ln = (Login) hs.getAttribute("user");
            System.out.println(ln.getUId());

            String fn = "";
            String lastn = "";
            String un = "";
            String state = "";
            String city = "";
            String area = "";
            String e = "";
            String ad1 = "";
            String ad2 = "";
            String num = "";
            String p = "";
            String des = "";
            String cmp = "";
            String work = "";
            String agentphoto = "";
            String agentname = "";
            int id = 0;

            // creates FileItem instances which keep their content in a temporary file on disk
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

            //get the list of all fields from request
            List<FileItem> fields = upload.parseRequest(request);
            // iterates the object of list
            Iterator<FileItem> it = fields.iterator();
            //getting objects one by one
            while (it.hasNext()) {
                //assigning coming object if list to object of FileItem
                FileItem fileItem = it.next();
                //check whether field is form field or not
                boolean isFormField = fileItem.isFormField();

                if (isFormField) {
                    //get the filed name 
                    String fieldName = fileItem.getFieldName();

                    if (fieldName.equals("fname")) {
                        fn = fileItem.getString();
                    } else if (fieldName.equals("id")) {
                        id = Integer.parseInt(fileItem.getString());
                    } else if (fieldName.equals("lname")) {
                        lastn = fileItem.getString();
                    } else if (fieldName.equals("uname")) {
                        un = fileItem.getString();
                    } else if (fieldName.equals("state")) {
                        state = fileItem.getString();
                    } else if (fieldName.equals("city")) {
                        city = fileItem.getString();
                    } else if (fieldName.equals("area")) {
                        area = fileItem.getString();
                    } else if (fieldName.equals("email")) {
                        e = fileItem.getString();
                    } else if (fieldName.equals("address1")) {
                        ad1 = fileItem.getString();
                    } else if (fieldName.equals("address2")) {
                        ad2 = fileItem.getString();
                    } else if (fieldName.equals("number")) {
                        num = fileItem.getString();

                    } else if (fieldName.equals("pwd")) {
                        p = fileItem.getString();
                    }

                    else if (fieldName.equals("descrip")) {
                        des = fileItem.getString();
                    } else if (fieldName.equals("compname")) {
                        cmp = fileItem.getString();
                    } else if (fieldName.equals("workx")) {
                        work = fileItem.getString();
                    }

                } else {

                    agentphoto = new File(fileItem.getName()).getName();

                    System.out.println(agentphoto);
                    try {

                        // FOR UBUNTU add GETRESOURCE  and GETPATH

                        String fp = "/home/rushin/NetBeansProjects/The_Asset_Consultancy/web/images/profilepic/";
                        //                    String filePath=  this.getServletContext().getResource("/images/profilepic").getPath()+"//";
                        System.out.println("====" + fp);
                        fileItem.write(new File(fp + agentphoto));
                    } catch (Exception ex) {
                        out.println(ex.toString());
                    }

                }

            }
            SessionFactory sf = NewHibernateUtil.getSessionFactory();
            Session ss = sf.openSession();
            Transaction tr = ss.beginTransaction();
            //            
            //           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("-------"+ar.get(0));
            //            }
            //            
            //            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 crr=ss.createCriteria(AgentDetail.class);
            //       crr.add(Restrictions.eq("uId", ln.getUId()));
            //       ArrayList<AgentDetail> arr=(ArrayList<AgentDetail>)crr.list();
            //       if(arr.isEmpty())
            //       {
            //           out.print("array empty");
            //       }
            //       else
            //       {
            //           AgentDetail agd=arr.get(0);
            AgentDetail agd2 = (AgentDetail) ss.get(AgentDetail.class, id);
            AgentDetail agd = new AgentDetail();

            agd.setUId(agd2.getUId());
            agd.setAId(agd2.getAId());
            agd.setACompanyname(cmp);
            agd.setADescription(des);
            agd.setAEmail(e);
            agd.setAFname(fn);
            agd.setAImg(agentphoto);
            agd.setALname(lastn);
            agd.setANo(num);
            agd.setAWorkx(work);
            agd.setACity(city);
            agd.setAArea(area);
            agd.setAState(state);
            agd.setAAddress1(ad1);
            agd.setAAddress2(ad2);
            agd.setAStatus(null);
            agd.setARating(null);
            agd.setAStatus("Accepted");
            // agd.getUId().setPwd(p);
            // agd.getUId().setUName(un);

            ss.evict(agd2);
            ss.update(agd);
            tr.commit();
            //       }

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

        }
    }

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

From source file:MainVehicle.java

public static void main(String h[]) {
    Vehicle v1 = new Vehicle();
    v1.setVid(1);/*from   w ww  .  j av a  2  s.com*/
    v1.setVname("some vehicle");

    TwoWheeler tw = new TwoWheeler();
    tw.setVid(12);
    tw.setVname("bike");
    tw.setHandleBar("bike handle bar");

    FourWheeler fw = new FourWheeler();
    fw.setVid(13);
    fw.setVname("car");
    fw.setSteeringWheel("car's streeing wheel");

    Configuration conf = new Configuration();
    conf.configure("hibernate.cfg.xml");
    SessionFactory factory = conf.buildSessionFactory();
    Session sess = factory.openSession();
    Transaction trx = sess.beginTransaction();
    sess.save(v1);
    sess.save(tw);
    sess.save(fw);

    trx.commit();
    sess.close();

}

From source file:proplist_serv.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w w w  .ja v 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 {
        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();

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

        ArrayList<PropDetail> pdl = (ArrayList<PropDetail>) cr.list();

        if (!pdl.isEmpty()) {
            request.setAttribute("pdl", pdl);

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

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

From source file:HibernateApp.java

License:Open Source License

private static Session createSession() {
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

    return sessionFactory.openSession();
}

From source file:add_this_user.java

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

        int uid = Integer.parseInt(request.getParameter("uid"));
        int gid = Integer.parseInt(request.getParameter("gid"));

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

        Login ln = (Login) ss.get(Login.class, uid);

        GroupDetail gd = (GroupDetail) ss.get(GroupDetail.class, gid);

        System.out.println("Group id is :" + gd.getGId());

        GmemberDetail gmd = new GmemberDetail();
        gmd.setGId(gd);
        gmd.setUId(ln);

        ss.save(gmd);
        tr.commit();
        RequestDispatcher rd = request.getRequestDispatcher("agents.jsp");
        rd.forward(request, response);

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

From source file:prop_fb_serv.java

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

    HttpSession hs = request.getSession();
    PrintWriter out = response.getWriter();
    try {

        if (hs.getAttribute("user") != null) {
            PropDetail pd = (PropDetail) hs.getAttribute("propobj");
            Login ln = (Login) hs.getAttribute("user");
            String content = request.getParameter("content");

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

            PropFeedback pf = new PropFeedback();

            pf.setPId(pd);
            pf.setUId(ln);
            pf.setComment(content);

            pf.setTime(new Date(System.currentTimeMillis()));

            ss.save(pf);
            tr.commit();

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

        }
    }

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