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

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

        Speciality sp = new Speciality();
        sp.setSpecName(request.getParameter("spec_name"));
        ss.save(sp);

        Qualification qf = new Qualification();
        qf.setQualificationName(request.getParameter("qualification_name"));
        ss.save(qf);

        Addressdetails ad = new Addressdetails();
        ad.setLine1(request.getParameter("line1"));
        ad.setLine2(request.getParameter("line2"));
        ad.setState(request.getParameter("state"));
        ad.setCity(request.getParameter("city"));
        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("doctor");
        ss.save(lo);

        Doctor dc = new Doctor();
        dc.setDFirstname(request.getParameter("d_firstname"));
        dc.setDLastname(request.getParameter("d_lastname"));
        dc.setEmailId(request.getParameter("email_id"));
        dc.setContactnum(request.getParameter("Cont_num"));
        dc.setGender(request.getParameter("gender"));
        dc.setStatus("not_active");
        dc.setBirthDate(request.getParameter("birth_date"));
        dc.setAddressid(ad);
        dc.setQualificationId(qf);
        dc.setSpecialityId(sp);
        dc.setUserId(lo);
        ss.save(dc);

        tr.commit();

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

        out.println("success");
    }

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

    finally {
        out.close();
    }
}

From source file:createAccount.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w  w w.  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()) {
        List allusers = list();

        String username = request.getParameter("new_username");
        String password = request.getParameter("new_password");
        SystemUser su = new SystemUser(allusers.size(), username, password);
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();

        session.beginTransaction();

        if (userExists(su.getUsername()) == null) {
            int id = (int) session.save(su);
            su.setId(id);
            session.getTransaction().commit();
            session.close();
            RequestDispatcher rdp = request.getRequestDispatcher("/success.jsp");
            rdp.forward(request, response);
        } else {
            //something about user already exists
            RequestDispatcher rdp = request.getRequestDispatcher("/fail.jsp");
            rdp.forward(request, response);
            System.out.println("Username already exists!");
        }
    }
}

From source file:createAccount.java

private static List list() {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();
    List userList = session.createQuery("FROM SystemUser").list();
    session.close();//from w  w  w.  j a  v a2 s . c o m
    return userList;
}

From source file:admin_add_lab.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 = 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("lId") != null) {

                int apid = Integer.parseInt(request.getParameter("lId"));
                Lab lab1 = (Lab) ss.get(Lab.class, apid);
                if (request.getParameter("status").equals("approve")) {

                    lab1.setStatus("ACTIVE");

                    ss.update(lab1);

                    String subject = "Your Appointment is Approved.!";
                    String content = "Hi," + lab1.getLabName() + " " + "\n"
                            + "Your Request has been approved by Admin" + ".\n";
                    String mail = lab1.getWebsite();

                    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")) {
                    lab1.setStatus("REJECTED");
                    ss.update(lab1);
                    String subject = "Your Request is Rejected.!";
                    String content = "Hi, " + lab1.getLabName() + " "
                            + "Your Request to join Cardiac Countermeasure has been rejected by admin"
                            + ".\n  ";
                    String mail = lab1.getWebsite();

                    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(Lab.class);
            //cr.add(Restrictions.eq("dId", a));
            cr.add(Restrictions.eq("status", "not Active"));
            ArrayList<Lab> da = (ArrayList<Lab>) cr.list();
            if (da.size() > 0) {
                request.setAttribute("da", da);
            }

            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("admin_add_lab.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:addDRO.java

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

        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);
        tr.commit();

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

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

From source file:viewblogs.java

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

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

@Test
public void testSave() {
    Purchase entity = new Purchase();
    entity.setId(0);//  w  ww  . jav a 2s.c om
    entity.setStatus(PurchaseStatus.W);

    PurchaseItemService sitem = new PurchaseItemService();
    sitem.setId(0);
    Service service = new Service();
    service.setId(1);
    sitem.setService(service);
    sitem.setFlagVat(FlagVat.I);
    entity.getServices().add(sitem);
    sitem = new PurchaseItemService();
    sitem.setId(0);
    service = new Service();
    service.setId(2);
    sitem.setService(service);
    sitem.setFlagVat(FlagVat.E);
    entity.getServices().add(sitem);
    PurchaseItemProduct pitem = new PurchaseItemProduct();
    pitem.setId(0);
    Product p = new Product();
    p.setId(1);
    pitem.setProduct(p);
    pitem.setFlagVat(FlagVat.I);
    entity.getProducts().add(pitem);

    pitem = new PurchaseItemProduct();
    pitem.setId(0);
    p = new Product();
    p.setId(2);
    pitem.setProduct(p);
    pitem.setFlagVat(FlagVat.I);
    entity.getProducts().add(pitem);
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Purchase data = (Purchase) session.get(Purchase.class, entity.getId());
    Purchase result = (Purchase) session.merge(entity);
    tran.commit();
}

From source file:GetGroupChatServ.java

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

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

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

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

        Criteria cr = ss.createCriteria(ChatMsg.class);
        cr.add(Restrictions.eq("gId", gd));
        cr.addOrder(Order.desc("cmid"));
        cr.setMaxResults(25);
        ArrayList<ChatMsg> cm = (ArrayList<ChatMsg>) cr.list();
        Collections.reverse(cm);

        System.out.println("size of the msg array :" + cm.size());
        request.setAttribute("msgs", cm);
        request.setAttribute("gobj", gd);

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

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

From source file:ShowProductServ.java

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

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

        request.setAttribute("pList", pList);
        RequestDispatcher rd = request.getRequestDispatcher("ProductList.jsp");
        rd.forward(request, response);

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

From source file:get_asset_records.java

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

        //            this file is currently used no where. It is just for the purpose of 
        //            getting assetrecords with the help of id
        //            

        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        String id = "10";
        Criteria cr = ss.createCriteria(AssetRecords.class);
        cr.add(Restrictions.eq("assetId", id));
        ArrayList<AssetRecords> ar = (ArrayList<AssetRecords>) cr.list();

        //            Query q = ss.createQuery("From AssetRecords");
        //            List<AssetRecords> ar = q.list();

        if (!ar.isEmpty()) {
            for (int i = 0; i < ar.size(); i++) {
                out.println(i + 1);
                out.println("year:" + ar.get(i).getYear());
                out.println("price:" + ar.get(i).getPrice());
                out.println();

            }

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