Example usage for org.hibernate Session save

List of usage examples for org.hibernate Session save

Introduction

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

Prototype

Serializable save(Object object);

Source Link

Document

Persist the given transient instance, first assigning a generated identifier.

Usage

From source file:HibernateCoursework.java

public void saveEmployee(Employee e) {
    System.out.println("Save Employee");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();/*from   w ww . j av  a2 s . com*/
    System.out.println("****************");

    hibernate.save(e);

    System.out.println("***Employee objects saved***");

    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed****");
}

From source file:HibernateCoursework.java

public void saveCustomer(Customer c) {

    System.out.println("Save Customer");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();//from w w  w. j av a 2  s  .  c  o m

    System.out.println("****************");

    hibernate.save(c);

    System.out.println("***Customer objects saved***");

    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");

}

From source file:HibernateCoursework.java

public void saveProject(Project p) {
    System.out.println("Save Project");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();//from  www  .  ja  v a2s.c  o  m

    System.out.println("****************");

    hibernate.save(p);

    System.out.println("***Project objects saved***");

    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:newMain.java

/**
 * @param args the command line arguments
 *//*from  ww  w  . jav a2  s  . 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();
}

From source file:Home.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);/*from w ww .ja  v  a2  s.  co  m*/

    System.out.println("run");

    //configureer connectie
    Configuration cfg = new Configuration();
    cfg.configure("Hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();
    Transaction tx = s.beginTransaction();

    //maak een employee 
    Scoutslid scouts = new Scoutslid();
    scouts.setNaam(request.getParameter("Naam"));
    scouts.setScoutsnaam(request.getParameter("Scoutsnaam"));
    scouts.setEmailadres(request.getParameter("Emailadres"));

    //bereid voor om weg te schrijven naar db
    s.save(scouts);
    s.flush();

    //schrijf weg
    tx.commit();

    //sluit connectie
    s.close();

    //haal de employees op
    //open een sessie
    s = sf.openSession();

    //Haal alle scoutsleden op
    List scoutsleden = s.createCriteria(Scoutslid.class).list();
    ArrayList<Scoutslid> aScout = new ArrayList();
    for (Iterator it1 = scoutsleden.iterator(); it1.hasNext();) {
        Scoutslid Sc = (Scoutslid) it1.next();
        aScout.add(Sc);

    }

    request.getSession().setAttribute("Scoutslid", aScout);

    //sluit de connectie
    s.close();

    request.getRequestDispatcher("results.jsp").forward(request, response);

}

From source file:patreg.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w w w.  java2  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 {
        String path = "refresh_page.jsp";
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        Login lo = new Login();
        lo.setUsername(request.getParameter("user_id"));
        lo.setPassword(request.getParameter("password"));
        lo.setRole("patient");
        ss.save(lo);

        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);

        Patient pi = new Patient();
        pi.setPFirstname(request.getParameter("p_firstname"));
        pi.setPLastname(request.getParameter("p_lastname"));
        pi.setGender(request.getParameter("gender"));
        pi.setBirthDate(request.getParameter("birth_date"));
        pi.setContactNum(request.getParameter("contact_num"));
        pi.setEmailId(request.getParameter("email_id"));
        pi.setAddressid(ad);
        pi.setUsedId(lo);
        ss.save(pi);

        tr.commit();

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

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

From source file:OrderSaveServ.java

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

        ProductTable pt = new ProductTable();
        pt.setPid(Integer.parseInt(request.getParameter("pid")));

        OrderDetails od = new OrderDetails();
        od.setPid(pt);
        od.setUserid(1);

        double amount = (Integer.parseInt(request.getParameter("qty")))
                * (Integer.parseInt(request.getParameter("prate")));
        od.setAmount(amount);
        System.out.println("Rate------" + request.getParameter("prate"));
        od.setQty(Integer.parseInt(request.getParameter("qty")));
        od.setTstatus("Pending");

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

        String pname = request.getParameter("pname");
        String prate = request.getParameter("prate");
        HttpSession hs = request.getSession();
        hs.setAttribute("OrderDetails", od);
        request.setAttribute("pname", pname);
        request.setAttribute("prate", prate);
        RequestDispatcher rd = request.getRequestDispatcher("PaymentPage.jsp");
        rd.forward(request, response);

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

From source file:reg_serv_agent.java

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

    try {

        String fn = request.getParameter("fname");
        String ln = request.getParameter("lname");
        String un = request.getParameter("uname");
        String e = request.getParameter("email");
        String stateid = request.getParameter("state");
        System.out.println("-------" + stateid);
        String cityid = request.getParameter("city");
        System.out.println("-------" + cityid);
        String r = request.getParameter("role");
        String p = request.getParameter("pwd");
        String areaid = request.getParameter("area");
        System.out.println("-------" + areaid);
        String no = request.getParameter("num");

        String adrs1 = request.getParameter("addrss");
        String adrs2 = request.getParameter("addrss2");

        Login li = new Login();

        li.setUName(un);
        li.setPwd(p);
        li.setURole("agent");

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

        ss.save(li);

        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);
        }

        AgentDetail ad = new AgentDetail();
        ad.setUId(li);
        ad.setAFname(fn);
        ad.setALname(ln);
        ad.setAAddress1(adrs1);
        ad.setAAddress2(adrs2);
        ad.setARating(0);

        ad.setAEmail(e);
        ad.setAState(state);
        ad.setACity(city);
        ad.setAArea(area);
        ad.setAStatus("Pending");
        ad.setANo(no);

        ss.save(ad);
        // out.println("Agent Registered");

        tr.commit();
        HttpSession session = request.getSession();
        session.setAttribute("user", li);

        RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
        rd.include(request, response);

    }

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

From source file:StudentJUnitTest.java

@Test
public void saveStudentToExistingTableTest() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;//from  www.ja  v  a2 s . com
    Integer id = null;

    try {
        tx = session.beginTransaction();

        Student new_student = new Student("Andrzej", "Wajda", "Mietczyska 34 00-242 Krk");
        id = (Integer) session.save(new_student);

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {

        session.close();
    }

}

From source file:StudentJUnitTest.java

@Test
public void usuwanieStudenta() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;/*from  ww w  . ja v a2s .c o m*/
    Integer idTemporaryStudent = null;

    /***********************/
    /*dodaj studenta do db*/
    /**********************/
    tx = session.beginTransaction();

    Student temporaryStudent = new Student("Karol", "Marzyciel", "Malinowa 23");
    idTemporaryStudent = (Integer) session.save(temporaryStudent);

    tx.commit();
    session.close();

    /****************************/
    /*usu nowododanego studenta*/
    /****************************/
    session = sessionFctry.openSession();
    tx = session.beginTransaction();
    try {
        Student usuwany = (Student) session.get(Student.class, idTemporaryStudent);
        session.delete(usuwany);

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {

        session.close();
    }

    /******************************/
    /*sprbuj pobra tego studenta*/
    /******************************/
    session = sessionFctry.openSession();
    tx = session.beginTransaction();

    Student poszukiwany = (Student) session.get(Student.class, idTemporaryStudent); //to powinno wyrzuci wyjtek

    assertNull(poszukiwany);

    tx.commit();
    session.close();

}