Java Code Examples for org.hibernate.Query#setFetchSize()

The following examples show how to use org.hibernate.Query#setFetchSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateTemplate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Prepare the given Query object, applying cache settings and/or
 * a transaction timeout.
 * @param queryObject the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareQuery(Query queryObject) {
	if (isCacheQueries()) {
		queryObject.setCacheable(true);
		if (getQueryCacheRegion() != null) {
			queryObject.setCacheRegion(getQueryCacheRegion());
		}
	}
	if (getFetchSize() > 0) {
		queryObject.setFetchSize(getFetchSize());
	}
	if (getMaxResults() > 0) {
		queryObject.setMaxResults(getMaxResults());
	}

	SessionHolder sessionHolder =
			(SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
	if (sessionHolder != null && sessionHolder.hasTimeout()) {
		queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds());
	}
}
 
Example 2
Source File: HibernateTemplate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Prepare the given Query object, applying cache settings and/or
 * a transaction timeout.
 * @param queryObject the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionFactoryUtils#applyTransactionTimeout
 */
protected void prepareQuery(Query queryObject) {
	if (isCacheQueries()) {
		queryObject.setCacheable(true);
		if (getQueryCacheRegion() != null) {
			queryObject.setCacheRegion(getQueryCacheRegion());
		}
	}
	if (getFetchSize() > 0) {
		queryObject.setFetchSize(getFetchSize());
	}
	if (getMaxResults() > 0) {
		queryObject.setMaxResults(getMaxResults());
	}
	SessionFactoryUtils.applyTransactionTimeout(queryObject, getSessionFactory());
}
 
Example 3
Source File: HibernateTemplate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare the given Query object, applying cache settings and/or
 * a transaction timeout.
 * @param queryObject the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionFactoryUtils#applyTransactionTimeout
 */
protected void prepareQuery(Query queryObject) {
	if (isCacheQueries()) {
		queryObject.setCacheable(true);
		if (getQueryCacheRegion() != null) {
			queryObject.setCacheRegion(getQueryCacheRegion());
		}
	}
	if (getFetchSize() > 0) {
		queryObject.setFetchSize(getFetchSize());
	}
	if (getMaxResults() > 0) {
		queryObject.setMaxResults(getMaxResults());
	}
	SessionFactoryUtils.applyTransactionTimeout(queryObject, getSessionFactory());
}
 
Example 4
Source File: HibernateTemplate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare the given Query object, applying cache settings and/or
 * a transaction timeout.
 * @param queryObject the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareQuery(Query queryObject) {
	if (isCacheQueries()) {
		queryObject.setCacheable(true);
		if (getQueryCacheRegion() != null) {
			queryObject.setCacheRegion(getQueryCacheRegion());
		}
	}
	if (getFetchSize() > 0) {
		queryObject.setFetchSize(getFetchSize());
	}
	if (getMaxResults() > 0) {
		queryObject.setMaxResults(getMaxResults());
	}

	SessionHolder sessionHolder =
			(SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
	if (sessionHolder != null && sessionHolder.hasTimeout()) {
		queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds());
	}
}
 
Example 5
Source File: HibernateTemplate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare the given Query object, applying cache settings and/or
 * a transaction timeout.
 * @param queryObject the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareQuery(Query queryObject) {
	if (isCacheQueries()) {
		queryObject.setCacheable(true);
		if (getQueryCacheRegion() != null) {
			queryObject.setCacheRegion(getQueryCacheRegion());
		}
	}
	if (getFetchSize() > 0) {
		queryObject.setFetchSize(getFetchSize());
	}
	if (getMaxResults() > 0) {
		queryObject.setMaxResults(getMaxResults());
	}

	SessionHolder sessionHolder =
			(SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
	if (sessionHolder != null && sessionHolder.hasTimeout()) {
		queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds());
	}
}
 
Example 6
Source File: AbstractSessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void initQuery(Query query, NamedQueryDefinition nqd) {
	query.setCacheable( nqd.isCacheable() );
	query.setCacheRegion( nqd.getCacheRegion() );
	if ( nqd.getTimeout()!=null ) query.setTimeout( nqd.getTimeout().intValue() );
	if ( nqd.getFetchSize()!=null ) query.setFetchSize( nqd.getFetchSize().intValue() );
	if ( nqd.getCacheMode() != null ) query.setCacheMode( nqd.getCacheMode() );
	query.setReadOnly( nqd.isReadOnly() );
	if ( nqd.getComment() != null ) query.setComment( nqd.getComment() );
}
 
Example 7
Source File: LookupTables.java    From unitime with Apache License 2.0 5 votes vote down vote up
/**
   * Executes the query to retrieve instructors
   * @param request
   * @param clause
   * @throws Exception
   */
  private static void getInstructors(HttpServletRequest request, SessionContext context, StringBuffer clause) throws Exception {
      String instructorNameFormat = UserProperty.NameFormat.get(context.getUser());
      
      Long acadSessionId = context.getUser().getCurrentAcademicSessionId();

StringBuffer query = new StringBuffer();
query.append("select distinct i from DepartmentalInstructor i ");
query.append(" where i.department.session.uniqueId = :acadSessionId ");
query.append(clause);
      
      DepartmentalInstructorDAO idao = new DepartmentalInstructorDAO();
org.hibernate.Session hibSession = idao.getSession();

Query q = hibSession.createQuery(query.toString());
q.setFetchSize(5000);
q.setCacheable(true);
q.setLong("acadSessionId", acadSessionId);
      
List result = q.list();
      Vector v = new Vector(result.size());
      Vector h = new Vector(result.size());
      Collections.sort(result);
   for (Iterator i=result.iterator();i.hasNext();) {
          DepartmentalInstructor di = (DepartmentalInstructor)i.next();
          String name = di.getName(instructorNameFormat);
          v.addElement(new ComboBoxLookup(name, di.getUniqueId().toString()));
          if (di.hasPreferences())
              h.add(di.getUniqueId());
}
      
      request.setAttribute(DepartmentalInstructor.INSTR_LIST_ATTR_NAME, v);
      request.setAttribute(DepartmentalInstructor.INSTR_HAS_PREF_ATTR_NAME, h);
  }
 
Example 8
Source File: MonitorServiceImpl.java    From fastdfs-zyc with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<Group> getAllGroups() throws IOException, MyException {
    List<Group> result = new ArrayList<Group>();
    Session session = getSession();
    String str = "from Group as g GROUP BY groupName order by g.created desc";
    Query query = session.createQuery(str);
    query.setFirstResult(0);
    query.setFetchSize(2);
    result = query.list();
    return result;
}
 
Example 9
Source File: HibernateDao.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * <p>Returns a List of <b>T</b> entities, where HQL clauses can be
 * specified.</p>
 * 
 * Note: This method is useful in read only. It can be use to delete or 
 * create <b>T</b> entities, but caution with <code>top</code> and 
 * <code>skip</code> arguments.
 * 
 * @param clauses query clauses (WHERE, ORDER BY, GROUP BY), if null no
 * clauses are apply.
 * @param skip number of entities to skip.
 * @param n  number of entities max returned.
 * @return a list of <b>T</b> entities.
 */
@SuppressWarnings ("unchecked")
public List<T> scroll (final String clauses, final int skip, final int n)
{
   StringBuilder hql = new StringBuilder ();
   hql.append ("FROM ").append (entityClass.getName ());
   if (clauses != null)
      hql.append (" ").append (clauses);

   Session session;
   boolean newSession = false;
   try
   {
      session = getSessionFactory ().getCurrentSession ();
   }
   catch (HibernateException e)
   {
      session = getSessionFactory ().openSession ();
      newSession = true;
   }

   Query query = session.createQuery (hql.toString ());
   if (skip > 0) query.setFirstResult (skip);
   if (n > 0) 
   {
      query.setMaxResults (n);
      query.setFetchSize (n);
   }
   
   logger.info("Execution of HQL: " + hql.toString ());
   long start = System.currentTimeMillis ();

   List<T> result = (List<T>) query.list ();
   logger.info("HQL executed in " + 
      (System.currentTimeMillis() -start) + "ms.");

   if (newSession)
   {
      session.disconnect ();
   }

   return result;
}
 
Example 10
Source File: InstructionalOffering.java    From unitime with Apache License 2.0 4 votes vote down vote up
/**
 * Search for instructional offerings
 * @param acadSessionId Academic Session
 * @param subjectAreaId Subject Area
 * @param courseNbr Course Number
 * @return TreeSet of results
 */
public static TreeSet<InstructionalOffering> search(
        Long acadSessionId,
        Long subjectAreaId,
        String courseNbr,
        boolean fetchStructure,
        boolean fetchCredits,
        boolean fetchInstructors,
        boolean fetchPreferences,
        boolean fetchAssignments,
        boolean fetchReservations) {

	org.hibernate.Session hibSession = (new InstructionalOfferingDAO()).getSession();

	StringBuffer query = new StringBuffer();
	query.append("select distinct io ");
	query.append(" from InstructionalOffering as io inner join io.courseOfferings as co ");

	if (fetchStructure) {
		query.append("left join fetch io.courseOfferings as cox ");
		query.append("left join fetch io.instrOfferingConfigs as ioc ");
		query.append("left join fetch ioc.schedulingSubparts as ss ");
		query.append("left join fetch ss.classes as c ");
		query.append("left join fetch ss.childSubparts as css ");
		query.append("left join fetch c.childClasses as cc ");
	}

	if (fetchCredits)
		query.append("left join fetch ss.creditConfigs as ssc ");

	if (fetchPreferences || fetchInstructors) {
		query.append("left join fetch c.classInstructors as ci ");
		query.append("left join fetch ci.instructor as di ");
	}

	if (fetchAssignments) {
		query.append("left join fetch c.assignments as ca ");
		query.append("left join fetch ca.rooms as car ");
	}

	if (fetchPreferences) {
		query.append("left join fetch c.preferences as cp ");
		query.append("left join fetch ss.preferences as ssp ");
		query.append("left join fetch di.preferences as dip ");
	}

	if (fetchReservations) {
		query.append("left join fetch ioc.individualReservations as ir ");
		query.append("left join fetch ioc.studentGroupReservations as sgr ");
		query.append("left join fetch ioc.acadAreaReservations as aar ");
		query.append("left join fetch ioc.posReservations as pr ");
	}

	query.append(" where io.session.uniqueId=:sessionId ");

	if (courseNbr != null && courseNbr.length() > 0){
		if (courseNbr.indexOf('*') >= 0) {
			query.append(" and co.courseNbr like :courseNbr ");
		} else {
			query.append(" and co.courseNbr = :courseNbr ");
		}
	}

	query.append(" and co.subjectArea.uniqueId = :subjectAreaId ");

	Query q = hibSession.createQuery(query.toString());
	q.setFetchSize(1000);
	q.setLong("subjectAreaId", subjectAreaId);
	q.setLong("sessionId", acadSessionId.longValue());
	if (courseNbr != null && courseNbr.length() > 0) {
		if (ApplicationProperty.CourseOfferingNumberUpperCase.isTrue())
           	courseNbr = courseNbr.toUpperCase();
		q.setString("courseNbr", courseNbr.replace('*', '%'));
	}
	q.setCacheable(true);


       TreeSet<InstructionalOffering> ts = new TreeSet<InstructionalOffering>(new InstructionalOfferingComparator(Long.valueOf(subjectAreaId)));

       long sTime = new java.util.Date().getTime();
	ts.addAll(q.list());
	long eTime = new java.util.Date().getTime();
       Debug.debug("fetch time = " + (eTime - sTime));

       return ts;
}