Example usage for org.hibernate Query setCacheMode

List of usage examples for org.hibernate Query setCacheMode

Introduction

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

Prototype

Query<R> setCacheMode(CacheMode cacheMode);

Source Link

Document

(Re)set the current CacheMode in effect for this query.

Usage

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);/*from w  ww  .j  a v  a2s  .co m*/
        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]);
        }/*  w  w w.j  a v a  2 s. co m*/
    }
    query.setCacheMode(CacheMode.IGNORE);
    query.setCacheable(false);
    query.setReadOnly(true);
    return query;
}

From source file:org.sakaiproject.profile2.dao.impl.ProfileDaoImpl.java

License:Educational Community License

/**
  * {@inheritDoc}//from  w  ww.ja  v  a  2 s .  c o  m
  */
public List<UserProfile> getUserProfiles(final int start, final int count) {

    //get fields directly from the sakaiperson table and use Transformers.aliasToBean to transform into UserProfile pojo
    //the idea is we *dont* want a SakaiPerson object
    HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {

            Query q = session.getNamedQuery(QUERY_GET_SAKAI_PERSON);

            //see scalars in the hbm
            q.setFirstResult(start);
            q.setMaxResults(count);
            q.setResultTransformer(Transformers.aliasToBean(UserProfile.class));
            q.setCacheMode(CacheMode.GET);
            return q.list();
        }
    };

    return (List<UserProfile>) getHibernateTemplate().executeFind(hcb);
}

From source file:org.squashtest.tm.service.internal.repository.hibernate.HibernateMilestoneDao.java

License:Open Source License

private ScrollableResults scrollableResults(Query query) throws HibernateException {
    return query.setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
}

From source file:org.yamj.core.database.dao.StagingDao.java

License:Open Source License

public List<VideoData> findVideoDatas(String baseName, StageDirectory stageDirectory) {
    if (stageDirectory == null) {
        return Collections.emptyList();
    }//  ww  w. j  av a  2s .c  om

    StringBuilder sb = new StringBuilder();
    sb.append("SELECT distinct vd ");
    sb.append("FROM VideoData vd ");
    sb.append("JOIN vd.mediaFiles mf ");
    sb.append("JOIN mf.stageFiles sf ");
    sb.append("WHERE sf.fileType=:fileType ");
    sb.append("AND mf.extra=:extra ");
    if (baseName != null) {
        sb.append("AND lower(sf.baseName)=:baseName ");
    }
    sb.append("AND sf.stageDirectory=:stageDirectory ");
    sb.append("AND sf.status != :deleted ");

    Query query = currentSession().createQuery(sb.toString());
    query.setParameter("fileType", FileType.VIDEO);
    query.setBoolean("extra", Boolean.FALSE);
    if (baseName != null) {
        query.setString("baseName", baseName.toLowerCase());
    }
    query.setParameter("stageDirectory", stageDirectory);
    query.setParameter("deleted", StatusType.DELETED);
    query.setCacheable(true);
    query.setCacheMode(CacheMode.NORMAL);
    return query.list();
}

From source file:org.yamj.core.database.dao.StagingDao.java

License:Open Source License

public List<VideoData> findVideoDatas(String baseName, Library library) {
    if (library == null) {
        return Collections.emptyList();
    }/*  www .j a v a  2 s  .  c  o m*/

    StringBuilder sb = new StringBuilder();
    sb.append("SELECT distinct vd ");
    sb.append("FROM VideoData vd ");
    sb.append("JOIN vd.mediaFiles mf ");
    sb.append("JOIN mf.stageFiles sf ");
    sb.append("JOIN sf.stageDirectory sd ");
    sb.append("WHERE sf.fileType=:fileType ");
    sb.append("AND mf.extra=:extra ");
    sb.append("AND lower(sf.baseName)=:baseName ");
    sb.append("AND sd.library=:library ");
    sb.append("AND sf.status != :deleted ");

    Query query = currentSession().createQuery(sb.toString());
    query.setParameter("fileType", FileType.VIDEO);
    query.setBoolean("extra", Boolean.FALSE);
    query.setString("baseName", baseName.toLowerCase());
    query.setParameter("library", library);
    query.setParameter("deleted", StatusType.DELETED);
    query.setCacheable(true);
    query.setCacheMode(CacheMode.NORMAL);
    return query.list();
}

From source file:org.yamj.core.database.dao.StagingDao.java

License:Open Source License

public List<VideoData> findVideoDatas(Collection<StageDirectory> stageDirectories) {
    if (CollectionUtils.isEmpty(stageDirectories)) {
        return Collections.emptyList();
    }// w w w . ja v a2s.c o  m

    StringBuilder sb = new StringBuilder();
    sb.append("SELECT distinct vd ");
    sb.append("FROM VideoData vd ");
    sb.append("JOIN vd.mediaFiles mf ");
    sb.append("JOIN mf.stageFiles sf ");
    sb.append("WHERE sf.fileType=:fileType ");
    sb.append("AND mf.extra=:extra ");
    sb.append("AND sf.stageDirectory in (:stageDirectories) ");
    sb.append("AND sf.status != :deleted ");

    Query query = currentSession().createQuery(sb.toString());
    query.setParameter("fileType", FileType.VIDEO);
    query.setBoolean("extra", Boolean.FALSE);
    query.setParameterList("stageDirectories", stageDirectories);
    query.setParameter("deleted", StatusType.DELETED);
    query.setCacheable(true);
    query.setCacheMode(CacheMode.NORMAL);
    return query.list();
}

From source file:org.yamj.core.database.dao.StagingDao.java

License:Open Source License

public StageFile findNfoFile(String searchName, StageDirectory stageDirectory) {
    if (stageDirectory == null) {
        return null;
    }/*from  ww  w  . jav  a2  s .  c om*/

    StringBuilder sb = new StringBuilder();
    sb.append("SELECT distinct sf ");
    sb.append("FROM StageFile sf ");
    sb.append("WHERE sf.fileType=:fileType ");
    sb.append("AND lower(sf.baseName)=:searchName ");
    sb.append("AND sf.stageDirectory=:stageDirectory ");
    sb.append("AND sf.status != :deleted ");

    Query query = currentSession().createQuery(sb.toString());
    query.setParameter("fileType", FileType.NFO);
    query.setString("searchName", searchName.toLowerCase());
    query.setParameter("stageDirectory", stageDirectory);
    query.setCacheable(true);
    query.setCacheMode(CacheMode.NORMAL);
    query.setParameter("deleted", StatusType.DELETED);
    return (StageFile) query.uniqueResult();
}

From source file:org.yamj.core.database.dao.StagingDao.java

License:Open Source License

public List<StageFile> getValidNFOFilesForVideo(long videoDataId) {
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT distinct sf ");
    sb.append("FROM StageFile sf ");
    sb.append("JOIN sf.nfoRelations nfrel ");
    sb.append("JOIN nfrel.nfoRelationPK.videoData vd ");
    sb.append("WHERE vd.id=:videoDataId ");
    sb.append("AND sf.fileType=:fileType ");
    sb.append("AND sf.status in (:statusSet) ");
    sb.append("ORDER by nfrel.priority DESC");

    Set<StatusType> statusSet = new HashSet<>();
    statusSet.add(StatusType.NEW);//from  w w  w  .ja  v  a2  s  .c o m
    statusSet.add(StatusType.UPDATED);
    statusSet.add(StatusType.DONE);

    Query query = currentSession().createQuery(sb.toString());
    query.setParameter("videoDataId", videoDataId);
    query.setParameter("fileType", FileType.NFO);
    query.setParameterList("statusSet", statusSet);
    query.setCacheable(true);
    query.setCacheMode(CacheMode.NORMAL);
    return query.list();
}

From source file:org.yamj.core.database.dao.StagingDao.java

License:Open Source License

public List<StageFile> getValidNFOFilesForSeries(long seriesId) {
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT distinct sf ");
    sb.append("FROM StageFile sf ");
    sb.append("JOIN sf.nfoRelations nfrel ");
    sb.append("JOIN nfrel.nfoRelationPK.videoData vd ");
    sb.append("JOIN vd.season sea ");
    sb.append("JOIN sea.series ser ");
    sb.append("WHERE ser.id=:seriesId ");
    sb.append("AND sf.fileType=:fileType ");
    sb.append("AND sf.status in (:statusSet) ");
    sb.append("ORDER by nfrel.priority DESC");

    Set<StatusType> statusSet = new HashSet<>();
    statusSet.add(StatusType.NEW);// w ww  .j  av  a2  s  .co m
    statusSet.add(StatusType.UPDATED);
    statusSet.add(StatusType.DONE);

    Query query = currentSession().createQuery(sb.toString());
    query.setParameter("seriesId", seriesId);
    query.setParameter("fileType", FileType.NFO);
    query.setParameterList("statusSet", statusSet);
    query.setCacheable(true);
    query.setCacheMode(CacheMode.NORMAL);
    return query.list();
}