Example usage for org.hibernate Session persist

List of usage examples for org.hibernate Session persist

Introduction

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

Prototype

void persist(Object object);

Source Link

Document

Make a transient instance persistent.

Usage

From source file:com.mycompany.thymeleafspringapp.dao.UsersDAOImpl.java

@Override
public Users createUser(String username, String password, String email) {
    Transaction tx = null;/*ww  w . j  a v  a  2  s . c  om*/
    Session session = null;
    try {
        session = sessionFactory.openSession();
        tx = session.beginTransaction();
        tx.setTimeout(5);
        Users user = new Users(0, username, password, email, Boolean.TRUE);
        session.persist(user);
        tx.commit();
        return user;

    } catch (RuntimeException e) {
        try {
            tx.rollback();
        } catch (RuntimeException rbe) {
            log.error("Couldnt roll back transaction", rbe);
        }
        throw e;
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:com.nec.crud.CrudRepositoryImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override
public void persist(final Session session, T entity) {
    session.persist(entity);
}

From source file:com.ntc.websystique5.program.HibernateStandAlone.java

public static void main(String args[]) {
    Student student = new Student("ram ", "sharan", "bct b");
    Address address = new Address("marchawar", "nyc", "nepal");

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/*w w w .j  a  va2  s .c  o  m*/

    session.persist(student);
    System.out.println("persisting in database: student");

    address.setAddress_id(student.getId());
    student.setAddress(address);

    session.save(student);
    System.out.println("saved in database: student");
    List<Student> students = (List<Student>) session.createQuery("from Student ").list();
    for (Student s : students) {
        System.out.println("Details " + s);
    }
    session.getTransaction().commit();
    session.close();
}

From source file:com.onlinebusiness.dao.UserDaoImpl.java

@Override
public void register(Customer c) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(c);
    session.flush();/*from w  w w  .  ja  va 2  s .  co  m*/
}

From source file:com.origen.hibernate.dao.general.DAOGeneral.java

public void agregar(T p) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(p);

}

From source file:com.payment.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        HttpSession session = request.getSession();

        Calendar cal = Calendar.getInstance();
        String month = new SimpleDateFormat("MMM").format(cal.getTime());
        String concept = request.getParameter("concept");
        String flat = (String) session.getAttribute("flat");
        // String month =  request.getParameter("month");
        int amount = Integer.parseInt(request.getParameter("amount"));

        Pay pay = new Pay();
        pay.setAmount(amount);/*from   w ww.ja va  2  s .c o m*/
        pay.setConcept(concept);
        pay.setFlat(flat);
        pay.setMonth(month);
        String email = (String) session.getAttribute("email");
        paymentmail pxx = new paymentmail();
        int n = pxx.send(email, amount, month);

        if (n == 0) {
            Configuration cfg = new Configuration();
            cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
            SessionFactory factory = cfg.buildSessionFactory();
            Session session1 = factory.openSession();
            Transaction t = session1.beginTransaction();
            session1.persist(pay);
            t.commit();
            session1.close();

            request.setAttribute("success", "suc");
            RequestDispatcher rd = request.getRequestDispatcher("account.jsp");
            rd.forward(request, response);

        } else {
            request.setAttribute("success", "err");
            RequestDispatcher rd = request.getRequestDispatcher("account.jsp");
            rd.forward(request, response);
        }

    }

    catch (Exception e) {

    }
}

From source file:com.pe.contable.dao.impl.UsuarioDaoImpl.java

@Override
public void save(ContUsuario usuario) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(usuario);
}

From source file:com.pojos.OrderUtil.java

public void PersistOrders(Map<Integer, Product> order_map, int order_number, Date order_date, Customer customer,
        float order_total, String store_orders_col, Date date_created, String order_comments, String order_ip,
        float order_tax_rate, Date invoice_date, String invoice_status, float invoice_amount, float finalCost,
        float sh_cost, float cartAmount) {

    Storeorders so = new Storeorders();
    so.setOrderNumber(order_number);//from   w  ww . j a v  a2 s .  com
    so.setOrderDate(order_date);
    so.setCustomer(customer);
    so.setOrderTotal(order_total);
    so.setStoreorderscol(store_orders_col);

    Orderitems oi = new Orderitems();

    for (Map.Entry<Integer, Product> entry : order_map.entrySet()) {

        oi.setStoreorders(so);
        oi.setProduct(entry.getValue());
        oi.setDateCreated(order_date);
    }

    Orderdetails od = new Orderdetails();
    od.setOrderComments(order_comments);
    od.setOrderTaxRate(order_tax_rate);
    od.setOrderIp(order_ip);
    od.setStoreorders(so);

    Orderinvoice o = new Orderinvoice();
    o.setInvoiceAmount(invoice_amount);
    o.setInvoiceDate(invoice_date);
    o.setInvoiceStatus(invoice_status);
    o.setStoreorders(so);

    /*float shipping_cost = Float.parseFloat(sh_cost);
    float cart_amount = Float.parseFloat(cartAmount);
    float final_cost = Float.parseFloat(finalCost);*/

    Orderamounts oa = new Orderamounts();
    oa.setOrderShipping(sh_cost);
    oa.setOrderSubtotal(cartAmount);
    oa.setOrderTotal(finalCost);
    oa.setStoreorders(so);

    Session s = sessionFactory.openSession();
    Transaction tx = s.beginTransaction();
    s.persist(so);
    s.persist(oi);
    s.persist(od);
    s.persist(oa);
    s.persist(o);
    s.getTransaction().commit();

}

From source file:com.pojos.RegisterUitility.java

public void PersistCustomer(String firstName, String lastName, String gender, String address, String userName,
        String password) {//from w  w  w.  j  a va 2  s.  com
    Customer customer = new Customer();
    Session s = sessionFactory.openSession();
    Transaction tx = s.beginTransaction();

    customer.setFirstName(firstName);
    customer.setLastName(lastName);
    // customer.setBillingaddresses(address);
    customer.setGender(gender);
    s.persist(customer);
    //   PersistCustomerCreds(customer,userName,password);
    Customercredentials ccreds = new Customercredentials();
    ccreds.setAccountActive(1);
    ccreds.setCustomer(customer);
    ccreds.setCustomerLogin(userName);
    ccreds.setCustomerPassword(password);

    s.persist(ccreds);

    s.getTransaction().commit();

}

From source file:com.pojos.RegisterUitility.java

public void PersistCustomerCreds(Customer customer, String userName, String password) {

    Session s = sessionFactory.openSession();
    Transaction tx = s.beginTransaction();

    Customercredentials ccreds = new Customercredentials();
    ccreds.setAccountActive(1);//from  w  w  w. ja va 2  s  .  c o  m
    ccreds.setCustomer(customer);
    ccreds.setCustomerLogin(userName);
    ccreds.setCustomerPassword(password);

    s.persist(ccreds);
    s.getTransaction().commit();

}