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:automatedbillingsoftware_DA.Templete_DA.java

public Templete fetchTempleteByName(String tempName) {
    SessionFactory sessionFactory = HibernateUtils.getLocSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction beginTransaction = session.beginTransaction();
    Query query = session.createQuery("from Templete where status=:status and name LIKE:name");
    query.setParameter("status", 1);
    query.setParameter("name", tempName);
    List<Templete> templeteList = (List<Templete>) query.list();
    beginTransaction.commit();//from w  w  w  .ja v a2  s  .co  m
    session.close();

    return templeteList != null && templeteList.size() > 0 ? templeteList.get(0) : null;
}

From source file:automatedbillingsoftware_DA.User_DA.java

public List<Users> fetchAllUser(Users users) {
    SessionFactory sessionFactory = HibernateUtils.getLocSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    Transaction beginTransaction = session.beginTransaction();
    Query query = session.createQuery("from Users where status=:status");
    query.setParameter("status", 1);

    List<Users> list = (List<Users>) query.list();
    //   session.saveOrUpdate(users);
    beginTransaction.commit();// w  ww. j ava  2  s  .c om
    //  return users;
    return list;
}

From source file:automatedbillingsoftware_DA.User_DA.java

public Users fetchUserById(int id) {
    SessionFactory sessionFactory = HibernateUtils.getLocSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    Transaction beginTransaction = session.beginTransaction();
    Query query = session.createQuery("from Users where status=:status and userid=:id");
    query.setParameter("status", 1);
    query.setParameter("userid", id);
    List<Users> list = (List<Users>) query.list();
    //   session.saveOrUpdate(users);
    beginTransaction.commit();//from w w  w  . j  a v  a2 s. co m
    //  return users;
    return (Users) list.get(0);
}

From source file:Baari.DbAccess.java

public void deleteRecipe(Recipe r) {
    //        if(session == null){
    //            session = alustaSessio();
    //        }/*w ww. jav a2s.c om*/
    //        verifyTransaction();
    //        boolean testing = false;
    //        System.out.println("Thn m vittu tkkn");
    //        while(!testing){
    //        try{
    //        session.delete(r);
    //        testing = true;
    //        }
    //        catch(org.hibernate.HibernateException e){
    //            System.out.println(" Catch a fish");
    //            session = istuntotehdas.
    //        }
    //        }
    //        transaction.commit();

    session = alustaSessio();
    verifyTransaction();
    Query query = session.createQuery("delete Recipe where recipe_id= :id");
    query.setParameter("id", r.getRecipe_id());
    query.executeUpdate();
    transaction.commit();
}

From source file:Baari.DbAccess.java

public void deleteDrink(Drink d) {

    session = alustaSessio();/*  w  w  w.  j  a va 2s.  c  om*/
    verifyTransaction();
    Query query = session.createQuery("delete Drink where drink_id= :id");
    query.setParameter("id", d.getDrink_id());
    query.executeUpdate();
    transaction.commit();

}

From source file:Baari.DbAccess.java

public void deleteRecipePart(RecipePart rp) {
    session = alustaSessio();//  www . j ava2  s.  c  o  m
    verifyTransaction();
    Query query = session.createQuery("delete RecipePart where recipePart_id= :id");
    query.setParameter("id", rp.getRecipePart_id());
    query.executeUpdate();
    transaction.commit();
    System.out.println("Poistin osan");

}

From source file:Baari.DbAccess.java

void updateIngredient(int id, String newName) {
    if (session == null) {
        session = alustaSessio();// w ww.j  a va 2s .  c  o m
    }
    verifyTransaction();
    Query query = session.createQuery("update Ingredient set name = :newName " + "where ingredient_id = :id");
    query.setParameter("newName", newName);
    query.setParameter("id", id);
    query.executeUpdate();
    transaction.commit();
}

From source file:backend.api.FinanceManagement.java

public List<Users> getUsers(int IdGroup) //ok
{
    Session s = SessionFactoryManager.INSTANCE.getSessionFactory().openSession();
    try {/*from   ww  w .j a  v a2  s. c  o  m*/
        String hql = "select new backend.core.model.Users(u.idusers, u.name, u.surname, u.password, u.salt, u.hired, u.last_login) "
                + "from Users u join u.Groups g where g.idgroups in (:idGroup)";
        Query q = s.createQuery(hql);
        q.setParameter("idGroup", IdGroup);

        List<Users> result = q.list();

        if (result.isEmpty() || result.size() == 0)
            return null;
        return result;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        s.close();
    }
}

From source file:baking.dao.BaseDao.java

License:Open Source License

/**
 * HQL?./*from  www  . ja  v a  2s  . c om*/
 * 
 * @param hql 
 * @param values ?
 * @return list
 */
public List find(final String hql, final Object[] values) {
    try {
        Session session = getSession();
        Query query = session.createQuery(hql);
        for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }
        return query.list();
    } catch (RuntimeException re) {
        throw re;
    }
}

From source file:baking.dao.BaseDao.java

License:Open Source License

/**
 * HQL?/*from  www. j  a v  a2 s. co m*/
 * @param hql HQL?
 * @param offset ???
 * @param values ?
 * @param pageSize ??
 * @return ?list
 */
public List findList(final String hql, final Object[] values, final int offset, final int pageSize) {
    try {
        Session session = getSession();
        Query query = session.createQuery(hql);
        for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }
        return query.setFirstResult(offset).setMaxResults(pageSize).list();
    } catch (RuntimeException re) {
        throw re;
    }
}