use of org.hibernate.SQLQuery in project KeDou by XiaoBai518.
the class BaseDao method find4PageBySql2.
public List<Map<String, Object>> find4PageBySql2(String sql, Map params, int pageNum, int pageSize) throws Exception {
SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
Iterator<String> keys = params.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
Object value = params.get(key);
if (value instanceof Collection)
query.setParameterList(key, (Collection) value);
else
query.setParameter(key, value);
}
query.setFirstResult((pageNum - 1) * pageSize);
query.setMaxResults(pageSize);
List<Map<String, Object>> list = query.list();
return list;
}
use of org.hibernate.SQLQuery in project KeDou by XiaoBai518.
the class BaseDao method findCount4PageBySql2.
public Long findCount4PageBySql2(String sql, Map params) throws Exception {
SQLQuery query = this.sessionFactory.getCurrentSession().createSQLQuery(sql);
Iterator<String> keys = params.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
Object value = params.get(key);
if (value instanceof Collection)
query.setParameterList(key, (Collection) value);
else
query.setParameter(key, value);
}
return Long.parseLong(query.uniqueResult().toString());
}
use of org.hibernate.SQLQuery in project openmrs-core by openmrs.
the class HibernatePersonDAO method getSavedPersonAttributeTypeName.
/**
* @see org.openmrs.api.db.PersonDAO#getSavedPersonAttributeTypeName(org.openmrs.PersonAttributeType)
*/
@Override
public String getSavedPersonAttributeTypeName(PersonAttributeType personAttributeType) {
SQLQuery sql = sessionFactory.getCurrentSession().createSQLQuery("select name from person_attribute_type where person_attribute_type_id = :personAttributeTypeId");
sql.setInteger("personAttributeTypeId", personAttributeType.getId());
return (String) sql.uniqueResult();
}
use of org.hibernate.SQLQuery in project openmrs-core by openmrs.
the class HibernateEncounterDAO method getSavedEncounterLocation.
/**
* @see org.openmrs.api.db.EncounterDAO#getSavedEncounterLocation(org.openmrs.Encounter)
*/
@Override
public Location getSavedEncounterLocation(Encounter encounter) {
Session session = sessionFactory.getCurrentSession();
FlushMode flushMode = session.getFlushMode();
session.setFlushMode(FlushMode.MANUAL);
try {
SQLQuery sql = session.createSQLQuery("select location_id from encounter where encounter_id = :encounterId");
sql.setInteger("encounterId", encounter.getEncounterId());
return Context.getLocationService().getLocation((Integer) sql.uniqueResult());
} finally {
session.setFlushMode(flushMode);
}
}
use of org.hibernate.SQLQuery in project candlepin by candlepin.
the class AbstractHibernateCurator method safeSQLUpdateWithCollection.
/**
* Performs a direct SQL update or delete operation with a collection by breaking the collection
* into chunks and repeatedly performing the update.
* <p></p>
* The parameter receiving the collection chunks must be the last parameter in the query and the
* provided collection must support the subList operation.
*
* @param sql
* The SQL statement to execute; must be an UPDATE or DELETE operation
*
* @param collection
* The collection to be broken up into chunks
*
* @return
* the number of rows updated as a result of this query
*/
protected int safeSQLUpdateWithCollection(String sql, Collection<?> collection, Object... params) {
int count = 0;
Session session = this.currentSession();
SQLQuery query = session.createSQLQuery(sql);
for (List<?> block : this.partition(collection)) {
int index = 1;
if (params != null) {
for (; index <= params.length; ++index) {
query.setParameter(String.valueOf(index), params[index - 1]);
}
}
query.setParameterList(String.valueOf(index), block);
count += query.executeUpdate();
}
return count;
}
Aggregations