Example usage for org.hibernate Session createQuery

List of usage examples for org.hibernate Session createQuery

Introduction

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

Prototype

@Override
    org.hibernate.query.Query createQuery(CriteriaDelete deleteQuery);

Source Link

Usage

From source file:bookstore.BookStoreManager.java

public void removeCategory() {
    try {/*ww  w. j  ava2 s.c  o  m*/
        //nhap id cua category muon xoa vao
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input id category want delete: ");
        int catID = Integer.parseInt(input.readLine());

        //Khoi tao va cho sesion do bat dau lam viec
        Session session = sf.openSession();
        session.beginTransaction();

        //Viet cau truy van HQL de xoa 1 category dua vao id cua category do
        Query query = session.createQuery("delete from Category where categoryid = :id");
        //set parameter cho cau truy van do bang phuong thuc setParameter
        query.setParameter("id", catID);
        //Moi thay doi them, xoa, sua deu dung executeUpdate() de thuc hien
        query.executeUpdate();

        //sau khi hoan thanh cau lenh thi commit
        session.getTransaction().commit();
        //sau cung thi dong session do lai
        session.close();
        System.out.println("DONE");

    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
}

From source file:bookstore.BookStoreManager.java

public void viewListCategory() {
    try {/*w w  w  . j  av  a2s. c  om*/

        //tao session va bat dau phien lam viec voi session do
        Session session = sf.openSession();
        session.beginTransaction();

        //Dung cau truy van hql de lay thong tin cua tat ca cac category co trong bang 
        String hql = "From Category";

        Query query = session.createQuery(hql);
        //Dua ket qua truy van ve thanh 1 list cac category bang phuong thuc list()
        List<Category> list = query.list();
        //duyet va in ra list category
        for (Category cat : list) {
            System.out.println(cat.toString());
        }
        //sau khi hoan thanh thi dong session do lai
        session.close();
    } catch (Exception e) {
    }
}

From source file:bookstore.BookStoreManager.java

public void updateCategory() {
    try {//www.  ja  v  a 2 s. c  om
        //nguoi dung se nhap ma so cua category muon update
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input id category want update: ");
        int catID = Integer.parseInt(input.readLine());

        //khoi tao 1 session va bat dau phien lam viec voi session do 
        Session session = sf.openSession();
        org.hibernate.Transaction tx = session.beginTransaction();

        //o day minh thay ten cua category thanh lap trinh
        String hql = "Update Category set name = 'Kiem Hiep 4' where categoryid = :catid";
        Query query = session.createQuery(hql);
        //set parameter cho category do
        query.setParameter("catid", catID);

        //ket qua tra ve se la mot so nguyen , neu bang 1 thi la thanh cong , neu la 0nghia la that bai
        int result = query.executeUpdate();
        tx.commit();
        System.out.println(result);
        //dong session sau khi update
        session.close();
        System.out.println("done");

    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
}

From source file:bookstore.BookStoreManager.java

public void viewListBookFromCategory() throws IOException {
    //Nhap id cua category muon xem
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Input category ID: ");
    int catid = Integer.parseInt(input.readLine());

    //Tao mot session moi
    Session session = sf.openSession();
    //cho session do bat dau lam viec
    session.beginTransaction();//from   w ww  .  jav  a  2  s  . com

    //Tao truy van HQL de lay category co categoryid bang vs gia tri nhap vao tu ban phim
    Query query = session.createQuery("from Category where categoryid = :catid");
    //set parameter catid bang voi gia tri nhap vao tu ban phim
    query.setParameter("catid", catid);
    //Lay duoc mot list cac category thoa man dieu kien bang phuong thuc list()
    List<Category> list = query.list();
    //duyet danh sach list do
    for (Category cat : list) {
        //Lay duoc tap hop cac cuon sach co cung categoryid
        Set<Book> setBook = cat.getBooks();
        //In ra thong tin cua cac cuon sach do
        for (Book b : setBook) {
            System.out.println(b.toString());
        }
    }
    //sau khi hoan thanh xong cau truy van thi nen dong session lai
    session.close();

    System.out.println("DONE");
}

From source file:bookstore.BookStoreManager.java

public void removeCategoryOneToMany() throws IOException {
    try {/*from w  w w .  jav  a  2  s . c  o  m*/
        //Tao mot session va bat dau lam viec voi session do
        Session session = sf.openSession();
        session.beginTransaction();

        //Nhap ma category muon xoa
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input ID Category Want Delete: ");
        int catid = Integer.parseInt(input.readLine());

        //Viet cau truy van HQL de xoa nhung cuon sach thuoc category can xoa
        String hql = "delete from Book where categoryid = :catid";
        Query query = session.createQuery(hql);
        //set parameter cho cau truy van do
        query.setParameter("catid", catid);
        //dung phuong thuc executeUpdate() de xoa nhung cuon sach thuoc category can xoa
        query.executeUpdate();

        //viet cau lenh HQL de xoa category co id ma nguoi dung nhap vao
        hql = "delete from Category where categoryid = :catid";
        Query q2 = session.createQuery(hql);
        //set parameter cho cau truy van do
        q2.setParameter("catid", catid);
        //Thuc thi cau lenh bang phuong thuc executeUpdate()
        q2.executeUpdate();
        session.getTransaction().commit();
        //Sau khi hoan thanh cong viec thi dong session lai
        session.close();
        System.out.println("Done");
    } catch (Exception e) {
    }
}

From source file:bookstore.CustomerUtils.java

public static CustomerDAO getCustomerByUsername(String username) {

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

    CustomerDAO customer = null;/*from  w w w. ja v  a  2  s.c  om*/

    //class    //db
    Query query = session.createQuery("from CustomerDaoHibernate C where C.uname = :uname ");
    //:uname
    query.setParameter("uname", username);
    List list = query.list();

    if (list != null && !list.isEmpty()) {
        return customer = (CustomerDAO) list.get(0);
    }

    return null;

}

From source file:bookstore.CustomerUtils.java

public static void deleteCustomer(int customerId) {

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

    Transaction tx = session.beginTransaction();
    String hql = "DELETE FROM CustomerDaoHibernate C WHERE C.id = :ID";
    Query query = session.createQuery(hql);
    query.setParameter("ID", customerId);

    query.executeUpdate();/*w  w w  . j a  v  a2  s.  c o  m*/
    tx.commit();

}

From source file:bookstore.dao.generic.GenericDAOImpl.java

public List findAll(Class clazz) {
    Session hibernateSession = this.getSession();
    List T = null;/*from   www . j a  v a 2 s  . c o  m*/
    Query query = hibernateSession.createQuery("from " + clazz.getName());
    T = query.list();
    return T;
}

From source file:bookstore.ProductUtils.java

public static ProductDAO getProductById(int id) {

    List<ProductDAO> products = null;

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

    Query query = session.createQuery("from ProductDaoHibernate P where P.id = :ID");
    query.setParameter("ID", id);

    products = query.list();/*from   w ww.j  a v  a  2 s.co m*/

    return products.get(0);
}

From source file:bookstore.ProductUtils.java

public static void deleteProductFromCategory(int productid, int catid) {

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

    Transaction tx = session.beginTransaction();
    String hql = "DELETE FROM ProductDaoHibernate P WHERE P.id = :ID";
    Query query = session.createQuery(hql);
    query.setParameter("ID", productid);

    query.executeUpdate();//from ww w. j a  va 2s . c om
    tx.commit();

}