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:com.onlineshopping.Models.CartService.java

public void saveCarts(List<Cart> carts) {
    long userId = 0;
    if (!carts.isEmpty()) {
        userId = carts.get(0).getUserId();
        deleteCarts(userId);/*from w  ww .  j a  va  2  s  . com*/
    }
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;
    try {
        tx = session.getTransaction();
        tx.begin();
        for (Cart cart : carts) {
            session.save(cart);
        }
        tx.commit();

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

From source file:com.onlineshopping.Models.CartService.java

private void deleteCarts(long userId) {
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;// w w w  .j av a2 s  . co  m
    try {
        tx = session.getTransaction();
        tx.begin();

        Query query = session.createQuery("delete from Cart where userid=:userid");
        query.setParameter("userid", userId);
        int deleted = query.executeUpdate();
        tx.commit();

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

From source file:com.onlineshopping.Models.LoginService.java

public User getUserByUsername(String username) {
    SessionFactory factory = HibernateUtil.getSessionFactory();

    Session session = factory.getCurrentSession();
    Transaction tx = null;/*  w w w  . ja v a  2  s  .co m*/
    User user = new User();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("select u from User u where username='" + username + "'");
        user = (User) query.uniqueResult();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        user = null;
        e.printStackTrace();
    }
    return user;
}

From source file:com.onlineshopping.Models.LoginService.java

public List<User> getListOfUsers() {
    List<User> list = new ArrayList<User>();
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;//w  w w .j  a  v  a  2 s .  c  o  m
    try {
        tx = session.getTransaction();
        tx.begin();
        list = session.createQuery("from User").list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    }
    return list;
}

From source file:com.onlineshopping.Models.ManufactureService.java

public static List<Manufacture> getManufactureList() {
    List<Manufacture> list = new ArrayList<Manufacture>();
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;/*from   w  w w . j a  v  a 2 s.  co  m*/
    try {
        tx = session.getTransaction();
        tx.begin();
        list = session.createQuery("from Manufacture").list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    }
    return list;
}

From source file:com.onlineshopping.Models.OperatingSystemService.java

public static List<OperatingSystem> getOperatingSystemList() {
    List<OperatingSystem> list = new ArrayList<>();
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;/*www . ja v a 2 s. c  om*/
    try {
        tx = session.getTransaction();
        tx.begin();
        list = session.createQuery("from OperatingSystem").list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    }
    return list;
}

From source file:com.onlineshopping.Models.ProductService.java

public List<Product> getProductByManufactureId(int manufactureId) {
    List<Product> list = new ArrayList<Product>();
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;//w ww . j  av a2 s.  co  m
    try {
        tx = session.getTransaction();
        tx.begin();
        list = session.createQuery("from Product where manufacture='" + manufactureId + "'").list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    }
    return list;
}

From source file:com.onlineshopping.Models.ProductService.java

public List<Product> getProductByOsId(int osId) {
    List<Product> list = new ArrayList<Product>();
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;/*ww w .  j  a va  2  s .c  om*/
    try {
        tx = session.getTransaction();
        tx.begin();
        list = session.createQuery("from Product where os='" + osId + "'").list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    }
    return list;
}

From source file:com.onlineshopping.Models.ProductService.java

public Product getProductById(int id) {
    SessionFactory factory = HibernateUtil.getSessionFactory();

    Session session = factory.getCurrentSession();
    Transaction tx = null;//from  ww w .  j a v  a  2  s.  co m
    Product product = new Product();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("select p from Product p where id='" + id + "'");
        product = (Product) query.uniqueResult();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        product = null;
        e.printStackTrace();
    }
    return product;
}

From source file:com.onlineshopping.Models.RegisterService.java

public User getUserByUsername(String username) {
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();

    Transaction tx = null;/*w w w.ja  v  a  2  s .c o m*/
    User user = new User();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("select u from User u where username='" + username + "'");
        user = (User) query.uniqueResult();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        user = null;
    }
    return user;
}