Example usage for org.hibernate Query setCacheable

List of usage examples for org.hibernate Query setCacheable

Introduction

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

Prototype

Query<R> setCacheable(boolean cacheable);

Source Link

Document

Enable/disable second level query (result) caching for this query.

Usage

From source file:org.openbravo.service.json.DataEntityQueryService.java

License:Open Source License

/**
 * @return a result which can be scrolled forward only and the results are not cached
 *///from   w  w w  .  j a  va2  s . c  o m
public ScrollableResults scroll() {
    final Query qry = buildOBQuery().createQuery();
    qry.setFetchSize(1000);
    qry.setCacheable(false);
    return qry.scroll(ScrollMode.FORWARD_ONLY);
}

From source file:org.openmrs.module.FacesRegister.api.db.hibernate.HibernateRegisterDAO.java

License:Open Source License

public List<Patient> getPatientsInLocation(Integer locationId) {
    /* //w  w  w.ja va  2  s.co m
     * get a list of patients given the location Id
     * 
     */
    List<Patient> patient = new ArrayList<Patient>();
    String strSQL = "select p.patient_id, p.voided ";
    strSQL += "from patient p ";
    strSQL += "join encounter e on e.patient_id=p.patient and e.voided=0 ";
    strSQL += "where p.voided=0 and e.location_id=:locationId; ";
    log.info("Executing Query" + strSQL);
    // For now am using a query with at least two columns. This makes my work easy for now 
    Query query = sessionFactory.getCurrentSession().createSQLQuery(strSQL);
    if (locationId != null)
        query.setInteger("locationId", locationId);

    query.setCacheable(true);
    List list = query.list(); //convert the generated list into an list object
    Iterator it = list.iterator();
    while (it.hasNext()) {
        Object[] row = (Object[]) it.next();
        Patient p = new Patient();
        p = Context.getPatientService().getPatient((Integer) row[0]); //call openmrs API
        patient.add(p);
    }
    return patient;
}

From source file:org.openmrs.module.FacesRegister.api.db.hibernate.HibernateRegisterDAO.java

License:Open Source License

public void query(Integer locationId, Date startDate, Date endDate) {

    StringBuilder strSQL = new StringBuilder();
    strSQL.append("select e.patient_id, date(o.value_datetime)as Date_Started_ART ");
    strSQL.append("from encounter e ");
    strSQL.append("join obs o on o.encounter_id=e.encounter_id and o.concept_id in (6746,6739) ");
    strSQL.append("where e.encounter_type in (21,22) ");
    strSQL.append("and e.location_id=" + locationId
            + " and date(o.value_datetime)  between :startDate and :endDate ");
    strSQL.append("union ");
    strSQL.append("select e.patient_id,e.encounter_datetime as Date_Started_ART ");
    strSQL.append("from encounter e ");
    strSQL.append(//from  w  w w  . j a v  a  2s  . com
            "join obs o on o.encounter_id=e.encounter_id and o.voided=0 and o.concept_id=1255 and o.value_coded=1256 ");
    strSQL.append("left outer join x_concept_name cn on cn.concept_id=o.value_coded ");
    strSQL.append("where e.location_id=:locationId ");
    //strSQL.append("and date(e.encounter_datetime) between '" + startDate +"' and '"+endDate+"' ");
    strSQL.append("and date(e.encounter_datetime) between :startDate and :endDate ");
    strSQL.append("and e.voided=0 ");

    log.info("Executing Query: " + strSQL.toString());

    //Passing parameters to the query
    Query queryResults = sessionFactory.getCurrentSession().createSQLQuery(strSQL.toString());
    if (startDate != null)
        queryResults.setDate("startDate", startDate);
    if (endDate != null)
        queryResults.setDate("endDate", endDate);
    if (locationId != null)
        queryResults.setInteger("locationId", locationId);

    queryResults.setCacheable(true);
    setQueryRst(queryResults);

}

From source file:org.openspaces.persistency.hibernate.iterator.DefaultScrollableDataIterator.java

License:Open Source License

protected ScrollableResults createCursor() {
    session = sessionFactory.openSession();
    session.setFlushMode(FlushMode.MANUAL);
    transaction = session.beginTransaction();
    if (entityName != null) {
        Criteria criteria = session.createCriteria(entityName);
        criteria.setCacheable(false);//w  w w . j  a  v  a 2 s  .  c  om
        criteria.setFlushMode(FlushMode.MANUAL);
        criteria.setFetchSize(fetchSize);
        if (perfromOrderById) {
            ClassMetadata metadata = sessionFactory.getClassMetadata(entityName);
            String idPropName = metadata.getIdentifierPropertyName();
            if (idPropName != null) {
                criteria.addOrder(Order.asc(idPropName));
            }
        }
        if (from >= 0) {
            if (from > 0)
                criteria.setFirstResult(from);
            criteria.setMaxResults(size);
        }
        return criteria.scroll(ScrollMode.FORWARD_ONLY);
    } else if (sqlQuery != null) {
        Query query = HibernateIteratorUtils.createQueryFromSQLQuery(sqlQuery, session);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else if (hQuery != null) {
        Query query = session.createQuery(hQuery);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        query.setCacheMode(CacheMode.IGNORE);
        query.setCacheable(false);
        query.setReadOnly(true);
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else {
        throw new IllegalStateException("No SQLQuery, entity, or Hibernate Query provided");
    }
}

From source file:org.openspaces.persistency.hibernate.iterator.HibernateIteratorUtils.java

License:Open Source License

public static Query createQueryFromSQLQuery(SQLQuery<?> sqlQuery, Session session) {
    String select = sqlQuery.getFromQuery();
    Query query = session.createQuery(select);
    Object[] preparedValues = sqlQuery.getParameters();
    if (preparedValues != null) {
        for (int i = 0; i < preparedValues.length; i++) {
            query.setParameter(i, preparedValues[i]);
        }//  www .ja  v  a2s  .c  o  m
    }
    query.setCacheMode(CacheMode.IGNORE);
    query.setCacheable(false);
    query.setReadOnly(true);
    return query;
}

From source file:org.osaf.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

public Ticket findTicket(String key) {
    if (key == null)
        throw new IllegalArgumentException("key cannot be null");

    try {//from   w  ww  .  jav  a2  s .  c  o m
        // prevent auto flushing when looking up ticket
        getSession().setFlushMode(FlushMode.MANUAL);
        Query hibQuery = getSession().getNamedQuery("ticket.by.key").setParameter("key", key);
        hibQuery.setCacheable(true);
        hibQuery.setFlushMode(FlushMode.MANUAL);
        return (Ticket) hibQuery.uniqueResult();
    } catch (HibernateException e) {
        getSession().clear();
        throw convertHibernateAccessException(e);
    }
}

From source file:org.osaf.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

protected HomeCollectionItem findRootItem(Long dbUserId) {
    Query hibQuery = getSession().getNamedQuery("homeCollection.by.ownerId").setParameter("ownerid", dbUserId);
    hibQuery.setCacheable(true);
    hibQuery.setFlushMode(FlushMode.MANUAL);

    return (HomeCollectionItem) hibQuery.uniqueResult();
}

From source file:org.osaf.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

public PasswordRecovery getPasswordRecovery(String key) {
    try {//from   w  w w  . j a v  a2 s.  c  om
        Query hibQuery = getSession().getNamedQuery("passwordRecovery.byKey").setParameter("key", key);
        hibQuery.setCacheable(true);
        return (PasswordRecovery) hibQuery.uniqueResult();
    } catch (HibernateException e) {
        getSession().clear();
        throw convertHibernateAccessException(e);
    }
}

From source file:org.osaf.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

private User findUserByUsernameIgnoreCase(String username) {
    Session session = getSession();//from w w w.j  ava  2s  . com
    Query hibQuery = session.getNamedQuery("user.byUsername.ignorecase").setParameter("username", username);
    hibQuery.setCacheable(true);
    hibQuery.setFlushMode(FlushMode.MANUAL);
    List users = hibQuery.list();
    if (users.size() > 0)
        return (User) users.get(0);
    else
        return null;
}

From source file:org.osaf.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

private User findUserByUsernameOrEmailIgnoreCaseAndId(Long userId, String username, String email) {
    Session session = getSession();//w w w  .java2 s  .  com
    Query hibQuery = session.getNamedQuery("user.byUsernameOrEmail.ignorecase.ingoreId")
            .setParameter("username", username).setParameter("email", email).setParameter("userid", userId);
    hibQuery.setCacheable(true);
    hibQuery.setFlushMode(FlushMode.MANUAL);
    List users = hibQuery.list();
    if (users.size() > 0)
        return (User) users.get(0);
    else
        return null;
}