Example usage for org.hibernate Session get

List of usage examples for org.hibernate Session get

Introduction

In this page you can find the example usage for org.hibernate Session get.

Prototype

Object get(String entityName, Serializable id);

Source Link

Document

Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.

Usage

From source file:GetGroupChatServ.java

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

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

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

        Session sessao = HibernateUtil.getSessionFactory().openSession();
        String nome = request.getParameter("nome");
        Professor prof = (Professor) sessao.get(Professor.class, nome);

        prof.setDisciplina(request.getParameter("disciplina"));
        prof.setSobrenome(request.getParameter("sobrenome"));

        Transaction tx = null;
        try {
            tx = sessao.beginTransaction();

            sessao.save(prof);
            sessao.flush();

            tx.commit();
            sessao.close();
            out.println("Professor Atualizado com sucesso.");

        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            out.println("Erro ao atualizar Professor: " + e.getMessage());
        }
    }
}

From source file:changepassword_lab.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from www .j a v a2s  . 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 original_password = null;
    try {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.getCurrentSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();

        String old_password = request.getParameter("current");
        String new_password = request.getParameter("new");

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

            Lab p1 = (Lab) hs.getAttribute("lab");
            Login l1 = (Login) ss.get(Login.class, p1.getUserId().getUserId());
            original_password = l1.getPassword();
            // out.println(original_password);

            if (original_password.equals(old_password)) {

                l1.setPassword(new_password);
                ss.update(l1);

                p1.setUserId(l1);
                hs.setAttribute("lab", p1);
                //  hs.setAttribute("patient", l1);
                tr.commit();
                request.setAttribute("msg", "Your password is updated.!");
                RequestDispatcher rd = request.getRequestDispatcher("change_password_lab.jsp");
                rd.forward(request, response);

            } else {
                request.setAttribute("msg1", "enter your current password correctly");
                RequestDispatcher rd = request.getRequestDispatcher("change_password_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:past_systemreport.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   w  ww  .  jav 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 = 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:CarregaOcorrencia.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w  ww.  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");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        int idOcorrencia = 0;

        Ocorrencia ocorrencia0;
        //Busar do banco...

        Ocorrencia ocorrencia;

        Session sessao = HibernateUtil.getSessionFactory().openSession();

        ocorrencia0 = (Ocorrencia) sessao.get(Ocorrencia.class, idOcorrencia);

        if (ocorrencia0 != null) {
            out.println("No encontrei o pincel de id: " + idOcorrencia);
        } else {
            out.println("dados da ocorrencia0:");
            out.println("Nome:" + ocorrencia0.getNome());
            out.println("Endereo:" + ocorrencia0.getEndereco());
            out.println("Tipo Ocorrencia:" + ocorrencia0.getTipo_ocorrencia());
            out.println("Idade:" + ocorrencia0.getIdade());
        }
    }
}

From source file:go_to_user.java

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

}

From source file:getstate.java

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

        Criteria cr = ss.createCriteria(StateMaster.class);
        ArrayList<StateMaster> stlist = (ArrayList<StateMaster>) cr.list();
        System.out.println("Size of State" + stlist.size());
        request.setAttribute("state", stlist);

        String id = request.getParameter("id");
        int i = Integer.parseInt(id);

        if (i == 1) {
            RequestDispatcher rd = request.getRequestDispatcher("customerregistration.jsp");
            rd.forward(request, response);
        } else if (i == 2) {

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

        else if (i == 3) {
            RequestDispatcher rd = request.getRequestDispatcher("property_add.jsp");
            rd.forward(request, response);
        }

        else if (i == 4) {

            Criteria cr2 = ss.createCriteria(AgentDetail.class);

            ArrayList<AgentDetail> adl = (ArrayList<AgentDetail>) cr2.list();

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

            }
            RequestDispatcher rd = request.getRequestDispatcher("agents.jsp");
            rd.forward(request, response);
        } else if (i == 5) {
            RequestDispatcher rd = request.getRequestDispatcher("customerprofile.jsp");
            rd.forward(request, response);
        } else if (i == 6) {
            RequestDispatcher rd = request.getRequestDispatcher("agentprofile.jsp");
            rd.forward(request, response);
        } else if (i == 7) {
            RequestDispatcher rd = request.getRequestDispatcher("trial_area.jsp");
            rd.forward(request, response);

        } else if (i == 8) {
            RequestDispatcher rd = request.getRequestDispatcher("analysis_search.jsp");
            rd.forward(request, response);
        } else if (i == 9) {
            Criteria cr3 = ss.createCriteria(PropDetail.class);

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

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

            }

            RequestDispatcher rd = request.getRequestDispatcher("property_search_1.jsp");
            rd.forward(request, response);
        } else if (i == 10) {
            int pid = Integer.parseInt(request.getParameter("pid"));
            PropDetail pd = (PropDetail) ss.get(PropDetail.class, pid);
            request.setAttribute("pd", pd);
            RequestDispatcher rd = request.getRequestDispatcher("prop_update.jsp");
            rd.forward(request, response);
        }
    }

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

From source file:buscaProduto.java

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

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

        Session sessao = HibernateUtil.getSessionFactory().openSession();

        Produto p = (Produto) sessao.get(Produto.class, id);

        sessao.close();

        out.println("Produto recuperado: <br/>");
        if (p != null) {
            out.println(p.getNome());
            out.println("<br/>");
            out.println(p.getPreco());
            out.println("<br/>");
            out.println(p.getIdentificador());
        } else {
            out.println("nenhum prod encontrado");
        }

    }
}

From source file:sendmessage.java

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

        Login ln = (Login) hs.getAttribute("user");

        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);
        String msg = request.getParameter("message");

        ChatMsg cm = new ChatMsg();
        cm.setGId(gd);
        cm.setMsg(msg);
        cm.setUId(ln);

        cm.setDateTime(new Date(System.currentTimeMillis()));

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

        //   cm.getDateTime(new Date(System.currentTimeMillis()));
        request.setAttribute("gid", gd.getGId());
        RequestDispatcher rd = request.getRequestDispatcher("GetGroupChatServ");
        rd.forward(request, response);

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

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

From source file:TestEmployee.java

@Test
public void saveEmployee() {
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Employee entity = new Employee();
    entity.setId(0);//from   ww w  .  j av a2s. co  m
    Education item = new Education();
    item.setId(0);
    item.setEmployeeId(null);
    item.setEducationLevel(null);
    entity.getEducations().add(item);
    Employee data = (Employee) session.get(Employee.class, entity.getId());
    Employee result = (Employee) session.merge(entity);
    System.out.println(result.getId());
    tran.commit();

    //Employee data =(Employee) session.get(Employee.class, 0);
    //Employee employee = (Employee) session.get(Employee.class, 3);
    //System.out.println(employee.getId());

}