Example usage for org.hibernate Query setString

List of usage examples for org.hibernate Query setString

Introduction

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

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setString(String name, String val) 

Source Link

Document

Bind a named String-valued parameter.

Usage

From source file:com.abiquo.server.core.infrastructure.storage.VolumeManagementDAO.java

License:Open Source License

public List<VolumeManagement> getVolumesByVirtualMachine(final VirtualMachine vm, final FilterOptions filters) {
    if (filters != null) {
        // Check if the orderBy element is actually one of the available ones
        VolumeManagement.OrderByEnum orderByEnum = VolumeManagement.OrderByEnum
                .valueOf(filters.getOrderBy().toUpperCase());

        String orderBy = defineOrderBy(orderByEnum.getColumnHQL(), filters.getAsc());

        Query query = getSession().getNamedQuery(VolumeManagement.VOLUMES_ATTACHED_TO_VM);

        String req = query.getQueryString() + orderBy;
        // Add order filter to the query
        Query queryWithOrder = getSession().createQuery(req);
        queryWithOrder.setInteger("vmId", vm.getId());
        queryWithOrder.setParameter("state", VolumeState.ATTACHED);
        queryWithOrder.setString("filterLike",
                filters.getFilter().isEmpty() ? "%" : "%" + filters.getFilter() + "%");

        Integer size = queryWithOrder.list().size();

        queryWithOrder.setFirstResult(filters.getStartwith());
        queryWithOrder.setMaxResults(filters.getLimit());

        PagedList<VolumeManagement> volumesList = new PagedList<VolumeManagement>(queryWithOrder.list());
        volumesList.setTotalResults(size);
        volumesList.setPageSize(filters.getLimit() > size ? size : filters.getLimit());
        volumesList.setCurrentElement(filters.getStartwith());

        return volumesList;
    } else {//  ww  w .j a  va  2  s.  c  o  m
        Criteria criteria = createCriteria(sameVirtualMachine(vm), sameState(VolumeState.ATTACHED));
        return getResultList(criteria);
    }
}

From source file:com.all.uberpeer.persistence.ContactDao.java

License:Apache License

public ContactInfo findContactByEmail(final String email) {
    return ht.execute(new HibernateCallback<ContactInfo>() {
        @Override//from  www.j av  a 2s  .  c o  m
        public ContactInfo doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("from User u where u.email=:email");
            query.setString("email", email);
            User user = (User) query.uniqueResult();
            if (user != null) {
                return new ContactInfo(user);
            }
            return null;
        }
    });

}

From source file:com.all.uberpeer.persistence.TrackDao.java

License:Apache License

public MusicTrack findTrackByHashcode(final String hashcode) {
    return ht.execute(new HibernateCallback<MusicTrack>() {
        @Override//from   w ww.jav  a 2  s .  c o  m
        public MusicTrack doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("from MUSIC_TRACKS u where u.hashcode=:hashcode");
            query.setString("hashcode", hashcode);
            MusicTrack track = (MusicTrack) query.uniqueResult();
            return track;
        }
    });

}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public List findBySQLQuery(final String sql, final Map map) {
    List data = getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(sql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }// w  w w. j  a  v  a 2s . co m
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            return query.list();
        }
    });
    return data;
}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public List findBySQLQuery(final String sql, final int begin, final int count, final Map map) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(sql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }/*from   w  ww  .  j a v a  2s.  c  o  m*/
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            if (begin >= 0) {
                query.setFirstResult(begin);
                query.setMaxResults(count);
            }
            return query.list();
        }
    });
}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public String findCountBySQLQuery(final String countSql, final Map map) {
    List data = getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(countSql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }//w  ww .  j ava 2 s .  c  o  m
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            return query.list();
        }
    });
    return data.get(0).toString();
}

From source file:com.anite.zebra.hivemind.impl.PriorityManager.java

License:Apache License

public void initializeService() {
    Query query = session.createQuery("from " + Priority.class.getName() + " where caption=:caption");

    // Find each value in the list
    query.setString("caption", LOW);
    List lowList = query.list();/*from   ww w  .ja  va2  s . c  o  m*/
    if (lowList.size() != 1) {
        Priority low = new Priority();
        low.setCaption(LOW);
        Transaction t = session.beginTransaction();
        session.save(low);
        t.commit();
    }

    query.setString("caption", NORMAL);
    List normalList = query.list();
    if (normalList.size() != 1) {
        Priority normal = new Priority();
        normal.setCaption(NORMAL);
        Transaction t = session.beginTransaction();
        session.save(normal);
        t.commit();
        this.defaultPriorityId = normal.getPriorityId();
    } else {
        Priority normal = (Priority) normalList.get(0);
        this.defaultPriorityId = normal.getPriorityId();
    }

    query.setString("caption", URGENT);
    List urgentList = query.list();
    if (urgentList.size() != 1) {
        Priority urgent = new Priority();
        urgent.setCaption(URGENT);
        Transaction t = session.beginTransaction();
        session.save(urgent);
        t.commit();
    }
}

From source file:com.app.gpo.dao.FieldDAO.java

License:Open Source License

public void delete(int id) {
    Query query = getSession().createSQLQuery("delete from field where fieldID = :fieldID");
    query.setString("fieldID", Integer.toString(id));
    query.executeUpdate();//from w  w  w .  j a  va  2  s  . c om
}

From source file:com.app.gpo.dao.FieldDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Field> findAllNotAssignToProductSlot(int id) {
    Query query = getSession().createQuery("from Field as f where f.fieldID not in "
            + "(select fm.field.fieldID from FieldMapping as fm where fm.productionSlot.productionSlotID = :productionSlotID)");
    query.setString("productionSlotID", Integer.toString(id));
    return query.list();
}

From source file:com.app.gpo.dao.FieldDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public Field findByfieldOriginID(String fieldOriginID) {
    Query query = getSession().createQuery("from Field as f where f.fieldOriginID = :fieldOriginID)");
    query.setString("fieldOriginID", fieldOriginID);
    return (Field) query.uniqueResult();
}