Example usage for org.hibernate SessionFactory getCurrentSession

List of usage examples for org.hibernate SessionFactory getCurrentSession

Introduction

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

Prototype

Session getCurrentSession() throws HibernateException;

Source Link

Document

Obtains the current session.

Usage

From source file:myprofile.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w  w w.jav 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 {
        /* TODO output your page here. You may use following sample code. */
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss1 = sf.getCurrentSession();
        Transaction tr1 = ss1.getTransaction();
        tr1.begin();
        HttpSession hs = request.getSession();
        if (hs.getAttribute("patient") != null) {

            Patient pi = (Patient) hs.getAttribute("patient");
            if (request.getParameter("btn_update") != null) {

                Addressdetails ad = pi.getAddressid();
                String line = request.getParameter("line1");
                ad.setLine1(line.trim());
                ad.setLine2(request.getParameter("line2").trim());
                ad.setPincode(request.getParameter("pincode").trim());
                ss1.update(ad);

                pi.setPFirstname(request.getParameter("p_firstname").trim());
                pi.setPLastname(request.getParameter("p_lastname").trim());
                pi.setGender(request.getParameter("gender").trim());
                pi.setBirthDate(request.getParameter("birth_date").trim());
                pi.setContactNum(request.getParameter("contact_num").trim());
                pi.setEmailId(request.getParameter("email_id").trim());

                ss1.update(pi);
                tr1.commit();
                hs.setAttribute("patient", pi);
                request.setAttribute("pdata", pi);
                RequestDispatcher rd = request.getRequestDispatcher("patient_myprofile.jsp");
                rd.forward(request, response);
            }

            else {

                request.setAttribute("pdata", pi);
                RequestDispatcher rd = request.getRequestDispatcher("patient_myprofile.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:writeblog.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w  w w . j a 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 ss1 = sf.getCurrentSession();
        Transaction tr1 = ss1.getTransaction();
        tr1.begin();
        HttpSession hs = request.getSession();
        if (hs.getAttribute("doctor") != null) {

            Doctor pi = (Doctor) hs.getAttribute("doctor");

            if (request.getParameter("subject") != null && request.getParameter("content") != null) {

                String sub = request.getParameter("subject");
                String con = request.getParameter("content");
                request.setAttribute("msg", "Blog Created..!");
                Blog b = new Blog();
                if (request.getParameter("updateblogid") != null) {
                    b.setBlogId(Integer.parseInt(request.getParameter("updateblogid")));
                    request.setAttribute("msg", "Blog Updated..!");
                }
                b.setBlogname(sub);
                b.setBlogdescription(con);
                b.setDId(pi);
                b.setBlogView(20);
                b.setImageUrl("abc.jpg");
                b.setBlogDate(new Date().toString());
                ss1.saveOrUpdate(b);
            }

            if (request.getParameter("blogid") != null) {

                Blog b = (Blog) ss1.get(Blog.class, Integer.parseInt(request.getParameter("blogid")));
                request.setAttribute("editblog", b);
            }

            Criteria cr1 = ss1.createCriteria(Blog.class);
            cr1.add(Restrictions.eq("dId", pi));
            ArrayList<Blog> dblog = (ArrayList<Blog>) cr1.list();
            if (dblog.size() > 0) {
                request.setAttribute("dblog", dblog);
            }

            tr1.commit();
            RequestDispatcher rd = request.getRequestDispatcher("doctor_write_blog1.jsp");
            rd.forward(request, response);

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

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

From source file:view_pat_profile_byLab.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* 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 ss1 = sf.getCurrentSession();
        Transaction tr1 = ss1.beginTransaction();
        HttpSession hs = request.getSession();

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

            if (request.getParameter("pid") != null) {

                int p_id = Integer.parseInt(request.getParameter("pid"));
                Patient p = (Patient) ss1.get(Patient.class, p_id);
                request.setAttribute("pdata", p);

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

            }

        }

    }

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

    finally {

        out.close();
    }
}

From source file:view_labprofile_admin.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// w w w .j  ava  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 ss1 = sf.getCurrentSession();
        Transaction tr1 = ss1.beginTransaction();
        HttpSession hs = request.getSession();

        if (hs.getAttribute("admin") != null) {
            if (request.getParameter("lid") != null) {

                int l_id = Integer.parseInt(request.getParameter("lid"));
                Lab d = (Lab) ss1.get(Lab.class, l_id);
                request.setAttribute("ldata", d);

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

            }

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

        }

    }

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

    finally {

        out.close();
    }
}

From source file:aa.webapp.servlet.hibernate.servlet.GetPersonByID.java

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

    int empId = Integer.parseInt(request.getParameter("empId"));
    logger.info("Request Param empId=" + empId);

    SessionFactory sessionFactory = (SessionFactory) request.getServletContext().getAttribute("SessionFactory");

    Session session = sessionFactory.getCurrentSession();
    Transaction tx = session.beginTransaction();
    Person emp = (Person) session.get(Person.class, new Long(empId));
    tx.commit();
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    if (emp != null) {
        out.print("<html><body><h2>Employee Details</h2>");
        out.print("<table border=\"1\" cellspacing=10 cellpadding=5>");
        out.print("<th>Employee ID</th>");
        out.print("<th>Employee Name</th>");
        out.print("<th>Employee Role</th>");

        out.print("<tr>");
        out.print("<td>" + empId + "</td>");
        out.print("<td>" + emp.getName() + "</td>");
        out.print("<td>" + emp.getRole() + "</td>");
        out.print("</tr>");
        out.print("</table></body><br/>");

        out.print("</html>");
    } else {
        out.print("<html><body><h2>No Employee Found with ID=" + empId + "</h2></body></html>");
    }

}

From source file:alpha.portal.dao.hibernate.PayloadDaoHibernate.java

License:Apache License

/**
 * Internal function to load the highest sequenceNumber from the AlphaCard
 * table.//from w  w w .  j a  v a 2s .  co m
 * 
 * @param sessionFactory
 *            the session factory
 * @param column
 *            the column
 * @return 0 if no record is found
 */
private Long getLastValue(final SessionFactory sessionFactory, final String column) {
    Session session;
    boolean sessionOwn = false;
    try {
        session = sessionFactory.getCurrentSession();
    } catch (final Exception e) {
        session = sessionFactory.openSession();
        sessionOwn = true;
    }
    final Query q = session.createSQLQuery("select max(" + column + ") from payload");
    final List<Object> list = q.list();
    BigInteger value = (BigInteger) list.get(0);
    if (value == null) {
        value = new BigInteger("0");
    }
    if (sessionOwn) {
        session.close();
    }
    return value.longValue();
}

From source file:app.core.Db.java

License:Open Source License

public static Session getSession() {
    SessionFactory sessionFactory = getSessionFactory();
    Assert.state(sessionFactory != null, "SessionFactory not injected");
    // Spring TransactionSynchronizationManager keeps the current session available for us via TransactionSynchronizationManager.getResource("sessionHolder").
    //        Assert.isTrue(TransactionSynchronizationManager.isActualTransactionActive(), "No need to go any further if no transaction.");
    try {//  w  ww . j  ava2s  . c  o m
        return sessionFactory.getCurrentSession();
    } catch (HibernateException e) {
        return sessionFactory.openSession();
    }
    //        return (Session) em().getDelegate();
}

From source file:app.core.Db.java

License:Open Source License

public static void flush() {
    // There must be a current session or else it is no point calling flush
    SessionFactory sessionFactory = getSessionFactory();
    sessionFactory.getCurrentSession().flush();
}

From source file:at.molindo.esi4j.module.hibernate.HibernateEntityResolver.java

License:Apache License

protected Session getCurrentSession(SessionFactory factory) {
    return factory.getCurrentSession();
}

From source file:automatedbillingsoftware_DA.Categories_DA.java

public Categories addCategories(Categories cat) {
    SessionFactory sessionFactory = HibernateUtils.getLocSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    Transaction beginTransaction = session.beginTransaction();
    //session.saveOrUpdate(cat);
    session.persist(cat);//from   w  ww .  j  a  va 2s  .  co  m

    beginTransaction.commit();
    return cat;
}