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:bo.com.offercruz.dal.impl.PermisoHibernateDAO.java

@Override
public List<Permiso> obtenerPermisosHijos(Integer idPermisoPadre) {
    Query query = getSession().createQuery("select p from Permiso p WHERE p.permisoPadreId = :idPermisoPadre ");
    query.setParameter("idPermisoPadre", idPermisoPadre);
    return query.list();
}

From source file:bo.com.offercruz.dal.impl.PermisoHibernateDAO.java

@Override
public List<Permiso> obtenerPermisosHijos(Integer idPermisoPadre, int idPerfil) {
    Query query = getSession().createQuery(
            "select p from Permiso p join p.perfils t WHERE p.permisoPadreId = :idPermisoPadre AND t.id = :idPerfil ");
    query.setParameter("idPerfil", idPerfil);
    query.setParameter("idPermisoPadre", idPermisoPadre);
    return query.list();
}

From source file:bo.com.offercruz.dal.impl.PermisoHibernateDAO.java

@Override
public List<Permiso> obtenerPermisosPadres(int tipo) {
    Query query = getSession()
            .createQuery("select p from Permiso p WHERE p.permisoPadreId = null AND p.tipo = :tipo ");
    query.setParameter("tipo", tipo);
    return query.list();
}

From source file:bo.com.offercruz.dal.impl.UsuarioHibernateDAO.java

@Override
public Integer getIdUsuarioPorCorreoElectronico(String email) {
    Query query = getSession().createQuery(
            "SELECT id FROM Usuario u WHERE u.correoElectronico = :CorreoElectronico AND u.estado > 0 ");
    query.setParameter("CorreoElectronico", email);
    return (Integer) query.uniqueResult();
}

From source file:bo.com.offercruz.dal.impl.UsuarioHibernateDAO.java

@Override
public Usuario logear(String usuario, String pass) {
    Query query = getSession()
            .createQuery("FROM Usuario u WHERE u.login = :Login AND u.password = :Password AND u.estado > 0 ");
    query.setParameter("Login", usuario);
    query.setParameter("Password", pass);
    return (Usuario) query.uniqueResult();
}

From source file:bo.com.offercruz.dal.impl.UsuarioHibernateDAO.java

@Override
public Integer getIdPorLogin(String login) {
    Query query = getSession().createQuery("SELECT id from Usuario u WHERE u.login = :Login AND u.estado > 0 ");
    query.setParameter("Login", login);
    return (Integer) query.uniqueResult();
}

From source file:bookstore.BookStoreManager.java

public void viewDetaiBook() {
    try {/*  ww w .  j  a v  a  2  s.  c om*/

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

        //Nhap ma so cua cuon sach muon xem chi tiet
        Scanner sc = new Scanner(System.in);
        System.out.println("Input ISBM book want view: ");
        int isbm = sc.nextInt();

        //Viet cau lenh HQL  de tim cuon sach do
        Query query = session.createQuery("From Book where isbm = :isbm");
        //set parameter cho isbm la isbm duoc nhap vao tu ban phim
        query.setParameter("isbm", isbm);
        //Dua book tim duoc vao trong list book bang phuong thuc list();
        List<Book> list = query.list();

        //in ra thong tin cua cuon sach va chi tiet cua cuon sach do thong qua ham getBoodetail()
        for (Book book : list) {
            System.out.println("ISBM: " + book.getIsbm() + " - Name: " + book.getName() + " - Number page: "
                    + book.getBookdetail().getNumpage() + " - Publisher: "
                    + book.getBookdetail().getPublisher());
        }
        //Sau khi thuc hien xong cau truy van thi nen dong session do lai
        session.close();
    } catch (Exception e) {
    }
}

From source file:bookstore.BookStoreManager.java

public void removeOneToOne() throws IOException {
    try {//from   ww  w .j  a v a2 s.  c  o m
        //Tao 1 session va bat dau lam viec voi no
        Session session = sf.openSession();
        session.beginTransaction();

        //Nguoi dung se nhap ma so cuon sach can xoa
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input isbm book want delete: ");
        int isbm = Integer.parseInt(input.readLine());

        //Viet cau lenh HQL de xoa chi tiet cua cuon sach do
        String hql = "delete from Bookdetail where isbm=:isbm";
        Query query = session.createQuery(hql);
        //Set parameter choi cau lenh vua viet
        query.setParameter("isbm", isbm);
        //moi thay doi nhu create, update , delete phai dung executeUpdate() de thuc hien

        query.executeUpdate();
        //sau khi xoa chi tiet cuon sach thi moi xoa duoc cuon sach do
        hql = "delete from Book where isbm=:isbm";
        query = session.createQuery(hql);
        query.setParameter("isbm", isbm);
        query.executeUpdate();

        session.getTransaction().commit();
        //sau khi xoa thi dong session lai
        session.close();
        System.out.println("Done");
    } catch (Exception e) {
    }
}

From source file:bookstore.BookStoreManager.java

public void viewBookOfAuthor() throws IOException {
    try {// w  w  w  .jav  a  2 s  .  c o  m
        //Nhap ten tac gia do vao
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input name author: ");
        String authorName = input.readLine();

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

        //viet cau lenh hql de lay duoc tac gia vua tao
        Query query = session.createQuery("from Author where name = :name");
        //set parameter cho cau lenh HQL tren
        query.setParameter("name", authorName);
        //Dua tac gia vua tao vao 1 list tac gia bang phuong thuc list();
        List<Author> list = query.list();

        System.out.println("List Book of " + authorName + " is: ");
        //duyet ten nhung cuon sach cua tac gia do
        for (Author author : list) {
            Set<Book> setBook = author.getBooks();
            for (Book b : setBook) {
                System.out.println(b.toString());
            }
        }
        session.close();
        System.out.println("DONE");
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
}

From source file:bookstore.BookStoreManager.java

public void viewAuthorOfBook() throws IOException {
    try {/*from  w  ww  .  jav a2 s  .co  m*/
        //Nhap ma cuon sach do vao
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input isbm of book: ");
        int idbook = Integer.parseInt(input.readLine());
        //tao 1 session va bat dau lam viec voi session do
        Session session = sf.openSession();
        session.beginTransaction();

        //Viet cau lenh HQL de tim cuon sach theo ma cuon sach
        Query query = session.createQuery("from Book where isbm= :idbook");
        //Set parameter cho cau lenh tren
        query.setParameter("idbook", idbook);
        //dua ket qua cua cau lenh tren vao danh list book bang phuong thuc list()
        List<Book> list = query.list();
        System.out.println("List Author of Book is: ");
        //Duyet danh sachc cac tac gia cua cuon sach do bang phuong thuc getAuthor
        for (Book book : list) {
            Set<Author> setAuthors = book.getAuthors();
            for (Author a : setAuthors) {
                System.out.println(a.toString());
            }
        }
        //Dong session lai sau khi xong viec
        session.close();
        System.out.println("DONE");
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
}