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

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w w  w . j ava 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();
        String code = "";

        Criteria cr = ss.createCriteria(Lab.class);
        ArrayList<Lab> all = (ArrayList<Lab>) cr.list();

        Criteria cr1 = ss.createCriteria(Lab.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("labName"));
            } else if (so.equals("2")) {
                cr1.addOrder(Order.desc("labName"));
            }
            //.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("labName", so + "%"));
        }
        ArrayList<Lab> all1 = (ArrayList<Lab>) cr1.list();
        if (all1.size() > 0) {
            for (int i = 0; i < all1.size(); i++) {
                Lab 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/Lab-Icon-245x300.png\" class=\"media-object news-img\">      \n"
                        + "                                    </div>\n"
                        + "                                    <div class=\"media-body\">\n"
                        + "                                        <h4 class=\"author-name\">" + dd.getLabName()
                        + "</h4>\n" + "                                        <a target='_blank' href='http://"
                        + dd.getWebsite() + "'> <span class=\"comments-date\">" + dd.getWebsite()
                        + "</span> </a>\n" + "                                        <p>"
                        + dd.getAddressid().getCityId().getCityName() + "</p>\n"
                        + "                                        <p>" + dd.getStatus() + "</p>\n"
                        + "                                        <div class=\"ui heart rating\" data-rating=\"1\" data-max-rating=\"3\"></div>\n"
                        + "                                        <a class=\"reply-btn\" href=\"view_labprofile_admin?lid="
                        + dd.getLId() + "\">View Profile</a>\n" + "                                    </div>\n"
                        + "                                </div>\n" + "                            </li>");
            }

        }
        request.setAttribute("dlist", all);

    }

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

From source file:TestCustomer.java

@Test
public void saveCustomer() {
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Customer entity = new Customer();
    entity.setId(0);//from w w  w .j  av a  2  s. c  om
    entity.setCustomerType(CustomerType.P);
    Customer data = (Customer) session.get(Customer.class, entity.getId());
    Customer result = (Customer) session.merge(entity);
    tran.commit();
    System.out.print(result.getId());
}

From source file:ProjectList.java

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

        page = request.getParameter("page");
        request.getSession().setAttribute("page", page);

        String role = "", path = "ProjectList.jsp";
        Criteria cr = null;
        ArrayList<ProjectDetails> ProjectList = new ArrayList<ProjectDetails>();
        ArrayList<ProjectUserDetails> ProjectUserList = null;

        LoginInfo loginInfo = (LoginInfo) request.getSession().getAttribute("loginInfo");
        int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        role = request.getSession().getAttribute("role").toString();

        HttpServletRequest request1 = (HttpServletRequest) request;
        String uri = request1.getRequestURI();
        switch (role) {
        case "G":
            ProjectDetails pd = new ProjectDetails();
            cr = ss.createCriteria(ProjectDetails.class);
            cr.add(Restrictions.eq("userIdFrom", loginInfo));
            ProjectList = (ArrayList<ProjectDetails>) cr.list();
            //                    System.out.println(ProjectList.size());
            break;

        default:
            ProjectUserDetails pudDRO = new ProjectUserDetails();
            cr = ss.createCriteria(ProjectUserDetails.class);
            cr.add(Restrictions.eq("userIdTo", loginInfo));
            ProjectUserList = (ArrayList<ProjectUserDetails>) cr.list();
            //                    System.out.println(ProjectUserList.size());

            //                    ProjectDetails mineId;
            //                    ProjectDetails pd1;
            //                    for (int i = 1; i <= ProjectUserList.size(); i++) {
            //                         mineId=(ProjectDetails) ProjectUserList.get(i).getMineId();                         
            ////                         pd1 = new ProjectDetails();
            ////                         cr = ss.createCriteria(ProjectDetails.class);
            ////                         cr.add(Restrictions.eq("mineId", mineId));                           
            //                         ProjectList.set(i, mineId);
            //                    }
            for (ProjectUserDetails pud : ProjectUserList) {
                Criteria cr3 = ss.createCriteria(ProjectDetails.class);
                cr3.add(Restrictions.eq("mineId", pud.getMineId().getMineId()));
                ArrayList<ProjectDetails> pList = (ArrayList<ProjectDetails>) cr3.list();

                if (pList.size() != 0) {
                    ProjectList.add(pList.get(0));
                }

            }
            break;
        }

        tr.commit();
        HttpSession hs = request.getSession();
        hs.setAttribute("ProjectList", ProjectList);
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);

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

From source file:agentrequestlist_serv.java

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

        Criteria cr = ss.createCriteria(AgentDetail.class);
        cr.add(Restrictions.eq("aStatus", status));
        ArrayList<AgentDetail> adl = (ArrayList<AgentDetail>) cr.list();

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

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

From source file:fetch_propfeedback.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w ww.ja v  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");
    PrintWriter out = response.getWriter();
    HttpSession hs = request.getSession();
    try {

        PropDetail pd = (PropDetail) hs.getAttribute("propobj");

        SessionFactory sf = util.NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();

        Criteria cr1 = ss.createCriteria(PropFeedback.class);
        cr1.add(Restrictions.eq("pId", pd));
        ArrayList<PropFeedback> pfl = (ArrayList<PropFeedback>) cr1.list();

        if (!pfl.isEmpty()) {

            // HttpSession hs1=request.getSession();
            System.out.println(pfl.size());
            System.out.println("inside the servlet else");
            System.out.println("inside the servlet else11");

            hs.setAttribute("pfl", pfl);
            System.out.println("inside the servlet else222");

        }

        else {
            System.out.println("no property feedback found");
            ;
        }

        RequestDispatcher rd = request.getRequestDispatcher("single.jsp");
        System.out.println("forwarding");
        rd.forward(request, response);

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

From source file:get_doctor_admin.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=\"view_docprofile_admin?did="
                        + dd.getDId() + "\">View profile</a>\n" + "                                    </div>\n"
                        + "                                </div>\n" + "                            </li>");
            }
        }
        request.setAttribute("dlist", all);

    }

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

    finally {
        out.close();
    }
}

From source file:viewblogs_user.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 = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();

        if (hs.getAttribute("patient") != null) {

            Blog b1 = new Blog();

            Criteria cr = ss.createCriteria(Blog.class);
            ArrayList<Blog> da = (ArrayList<Blog>) cr.list();
            if (da.size() > 0) {

                request.setAttribute("da", da);
            }

            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("blogs_user.jsp");
            rd.forward(request, response);

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

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

}

From source file:getcity.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");

    PrintWriter out = response.getWriter();

    try {

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

        int stateid = Integer.parseInt(request.getParameter("state"));
        out.print(stateid);

        StateMaster sm = new StateMaster();
        sm.setSId(stateid);

        Criteria cr = ss.createCriteria(CityMaster.class);
        cr.add(Restrictions.eq("sId", sm));

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

        for (int i = 0; i < ctlist.size(); i++) {
            CityMaster cm = ctlist.get(i);
            System.out.println("------------- getcity" + cm.getCityName());
            System.out.println("------------- " + cm.getCityId());
            out.print("<option value=" + cm.getCityId() + ">" + cm.getCityName() + "</option>");
        }

        tr.commit();

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

From source file:ViewProject.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  w w w . jav  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();
    String path = "";

    RequestDispatcher rd;
    try {
        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();
        int mineId = Integer.parseInt(request.getParameter("mineId").toString());
        Criteria cr = ss.createCriteria(ProjectDetails.class);
        cr.add(Restrictions.eq("mineId", mineId));

        ArrayList<ProjectDetails> PDlist = (ArrayList<ProjectDetails>) cr.list();
        ProjectDetails pd = PDlist.get(0);
        tr.commit();

        request.setAttribute("projDet", pd);
        hs.setAttribute("projDet", pd);
        path = "viewProject.jsp";
        rd = request.getRequestDispatcher(path);

    } catch (Exception e) {
        path = "FailureME.jsp";
        rd = request.getRequestDispatcher(path);
    }
    rd.forward(request, response);
}

From source file:newMain.java

/**
 * @param args the command line arguments
 *//*  ww w  . j  a va 2s.  c o m*/
public static void main(String[] args) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();

    //    Utilisateur u = (Utilisateur) session.get(Utilisateur.class,2);
    //  Chantier c = (Chantier) session.get(Chantier.class,2);

    Utilisateur u = new Utilisateur(1, "Graywynd", "rootpass", "Khiari", "Seif", "54659879", "seif@gmail.com",
            0, null, null, null, null, null, null, null, null, null);

    session.save(u);

    session.getTransaction().commit();
    session.close();
}