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.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 ww  w . j a  va 2s.  c o  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();//  w w w .j  a  v  a2 s.c  o m
    tx.commit();

}

From source file:br.com.apprestaurante.dao.CategoriaProdutoDao.java

public List<CategoriaProduto> buscarPorRestaurante(Integer codigoRestaurante) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transacao = null;//from   w w  w. ja  va 2 s . c  om
    List<CategoriaProduto> lista = null;

    try {
        transacao = session.beginTransaction();
        Query query = session.createQuery(
                "Select distinct p.categoriaProduto from Produto as p where p.restaurante.codigo = :codigoRestaurante ");
        query.setParameter("codigoRestaurante", codigoRestaurante);
        lista = query.list();
        transacao.commit();
        return lista;
    } catch (HibernateException e) {
        if (transacao != null) {
            transacao.rollback();
        }
        e.printStackTrace();
        throw new HibernateException(e.getMessage());
    } finally {
        session.close();
    }
}

From source file:br.com.apprestaurante.dao.CategoriaProdutoDao.java

public List<CategoriaProduto> buscarPorRestauranteUsuario(Integer codigoRestaurante) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transacao = null;/*  w  ww . ja v a 2 s  . c o  m*/
    List<CategoriaProduto> lista = null;

    try {
        transacao = session.beginTransaction();
        Query query = session.createQuery(
                "Select distinct p.categoriaProduto from Produto as p where p.restaurante.codigo = :codigoRestaurante and p.cancelado = 0 ");
        query.setParameter("codigoRestaurante", codigoRestaurante);
        lista = query.list();
        transacao.commit();
        return lista;
    } catch (HibernateException e) {
        if (transacao != null) {
            transacao.rollback();
        }
        e.printStackTrace();
        throw new HibernateException(e.getMessage());
    } finally {
        session.close();
    }
}

From source file:br.com.apprestaurante.dao.CategoriaProdutoDao.java

public List<CategoriaProduto> buscarPorProdutoNome(String nome, Integer codigoRestaurante) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transacao = null;//from   w  w w.j  a  v a 2  s.  c o m
    List<CategoriaProduto> lista = null;

    try {
        transacao = session.beginTransaction();
        Query query = session.createQuery(
                "Select distinct p.categoriaProduto from Produto as p where p.nome like :nome and p.restaurante.codigo = :codigoRestaurante ");
        query.setParameter("nome", "%" + nome + "%");
        query.setParameter("codigoRestaurante", codigoRestaurante);
        lista = query.list();
        transacao.commit();
        return lista;
    } catch (HibernateException e) {
        if (transacao != null) {
            transacao.rollback();
        }
        e.printStackTrace();
        throw new HibernateException(e.getMessage());
    } finally {
        session.close();
    }
}

From source file:br.com.apprestaurante.dao.CategoriaProdutoDao.java

public List<Produto> getAllByNome(String nome) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transacao = null;//from  w  w  w.j  a va  2 s . c  om
    List<Produto> lista = null;

    try {
        transacao = session.beginTransaction();
        Query query = session.createQuery("from Produto as r where r.nome like :nome");
        query.setParameter("nome", "%" + nome + "%");
        lista = query.list();
        transacao.commit();
        return lista;
    } catch (HibernateException e) {
        if (transacao != null) {
            transacao.rollback();
        }
        e.printStackTrace();
        throw new HibernateException(e.getMessage());
    } finally {
        session.close();
    }

}

From source file:br.com.arsmachina.authentication.dao.hibernate.UserDAOImpl.java

License:Apache License

/**
 * Finds the user with a given login and password. The login search is case-insensitive.
 * //from   www.  ja v a  2s .c om
 * @see br.com.arsmachina.authentication.dao.UserDAO#findByLoginAndPassword(java.lang.String,
 * java.lang.String)
 */
public User findByLoginAndPassword(String login, String password) {

    Session session = getSession();

    Query query = session
            .createQuery("from User where lowercase(login) = :login and " + "password = :password");

    query.setParameter("login", login.toLowerCase());
    query.setParameter("password", passwordEncrypter.encrypt(password));

    return (User) query.uniqueResult();

}

From source file:br.com.arsmachina.authentication.dao.hibernate.UserDAOImpl.java

License:Apache License

/**
 * Finds an user by its login. The search is case-insensitive.
 * //from w  w w .j  av a  2  s  .co  m
 * @see br.com.arsmachina.authentication.dao.UserDAO#findByLogin(java.lang.String)
 */
public User findByLogin(String login) {

    Session session = getSession();

    Query query = session.createQuery("from User where lower(login) = :login");
    query.setParameter("login", login.toLowerCase());

    return (User) query.uniqueResult();

}

From source file:br.com.arsmachina.authentication.dao.hibernate.UserDAOImpl.java

License:Apache License

public boolean hasUserWithLogin(String login) {

    Query query = getSession().createQuery("select count (distinct u) from User u where lower(login) = :login");
    query.setParameter("login", login.toLowerCase());

    Long result = (Long) query.uniqueResult();

    return result > 0;

}

From source file:br.com.arsmachina.dao.hibernate.WriteableDAOImpl.java

License:Apache License

public void delete(K id) {

    Query query = getSession().createQuery(deleteHQL);
    query.setParameter("id", id);
    query.executeUpdate();/*w w w . j  a va2 s  .  c o m*/

}