Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

In this page you can find the example usage for org.hibernate Query setParameter.

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

From source file:bookstore.BookStoreManager.java

public void removeCategory() {
    try {/*from   w ww .jav a 2 s.co 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 updateCategory() {
    try {/*  ww  w .  j a  v a2 s  .  c o m*/
        //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();// ww  w.  j a  v a 2 s . c  o m
    //cho session do bat dau lam viec
    session.beginTransaction();

    //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 .ja  va  2s.  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.CategoryUtils.java

public static CategoryDAO getCategoryById(int catId) {

    List<CategoryDAO> categories = null;

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

    Query query = session.createQuery("from CategoryDaoHibernate C where C.id = :ID");
    query.setParameter("ID", catId);

    categories = query.list();/* w  w  w  .ja  va  2 s .c om*/

    if (categories != null && !categories.isEmpty()) {
        return categories.get(0);
    }

    return null;
}

From source file:bookstore.CategoryUtils.java

public static void deleteCategory(int catId) {

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

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

    query.executeUpdate();/*from   www. ja va 2s . c  o m*/
    tx.commit();

}

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 a2  s  .c  o  m*/

    //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();/*from  w w w .j a  v a 2  s.  co m*/
    tx.commit();

}

From source file:bookstore.CustomerUtilsTest.java

@After
public void tearDown() {

    log.info("deleting testuser");

    Transaction tx = session.beginTransaction();

    String hql = "DELETE FROM CustomerDaoHibernate C WHERE C.id = :ID ";
    Query query = session.createQuery(hql);
    query.setParameter("ID", CustomerUtils.getCustomerByUsername("testuser").getId());
    query.executeUpdate();/*from   w w w .j a v a  2s.c om*/
    tx.commit();
}

From source file:bookstore.dao.moneda.MonedaDAOImpl.java

public Moneda findByMonedaCod(String monedaCod) {
    String hql = "from Moneda where monedaIsoCod = :monedaIsoCod";
    Query query = HibernateUtil.getSession().createQuery(hql);
    query.setParameter("monedaIsoCod", monedaCod);
    List resultados = query.list();
    return (Moneda) resultados.get(0);
}