Пример #1
0
  public FortunePage() {
    Session session = HibernateUtil.getSessionFactory().openSession();

    Query query = session.createQuery("from Fortune");
    query.setReadOnly(true);
    List list = query.list();
    List<Fortune> fortunes = new ArrayList<Fortune>(list);
    session.close();

    Fortune newFortune = new Fortune();
    newFortune.message = "Additional fortune added at request time.";
    fortunes.add(newFortune);

    sort(fortunes);

    ListView<Fortune> listView =
        new ListView<Fortune>("fortunes", fortunes) {
          @Override
          protected void populateItem(ListItem<Fortune> item) {
            Fortune fortune = item.getModelObject();
            item.add(new Label("id", fortune.id));
            item.add(new Label("message", fortune.message));
          }
        };
    add(listView);
  }
  private <T> List<T> executeQuery(
      Query query, int numColumns, LastModificationDateFactory<T> lastModificationDateFactory) {

    query.setReadOnly(true);
    query.setCacheable(false);

    ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY);

    final List<T> dates = new ArrayList<>();
    int resultsCount = 0;
    int datesCount = 0;
    while (scrollableResults.next()) {
      resultsCount++;
      Object[] row = scrollableResults.get();

      T date = extractLastModificationDate(row, numColumns, lastModificationDateFactory);

      if (date != null) {
        dates.add(date);
        datesCount++;
      }
    }

    if (resultsCount > datesCount) {
      LOGGER.warn(
          "Got {} in result set and {} dates. query= {}",
          resultsCount,
          datesCount,
          query.getQueryString());
    } else {
      LOGGER.debug("Got {} in result set and {} dates.", resultsCount, datesCount);
    }

    return dates;
  }
  private Query createQuery(
      String queryString, @Nullable QueryModifiers modifiers, boolean forCount) {
    Query query = session.createQuery(queryString);
    HibernateUtil.setConstants(query, getConstants(), getMetadata().getParams());
    if (fetchSize > 0) {
      query.setFetchSize(fetchSize);
    }
    if (timeout > 0) {
      query.setTimeout(timeout);
    }
    if (cacheable != null) {
      query.setCacheable(cacheable);
    }
    if (cacheRegion != null) {
      query.setCacheRegion(cacheRegion);
    }
    if (comment != null) {
      query.setComment(comment);
    }
    if (readOnly != null) {
      query.setReadOnly(readOnly);
    }
    for (Map.Entry<Path<?>, LockMode> entry : lockModes.entrySet()) {
      query.setLockMode(entry.getKey().toString(), entry.getValue());
    }
    if (flushMode != null) {
      query.setFlushMode(flushMode);
    }

    if (modifiers != null && modifiers.isRestricting()) {
      if (modifiers.getLimit() != null) {
        query.setMaxResults(modifiers.getLimit().intValue());
      }
      if (modifiers.getOffset() != null) {
        query.setFirstResult(modifiers.getOffset().intValue());
      }
    }

    // set transformer, if necessary
    List<? extends Expression<?>> projection = getMetadata().getProjection();
    if (projection.size() == 1 && !forCount) {
      Expression<?> expr = projection.get(0);
      if (expr instanceof FactoryExpression<?>) {
        query.setResultTransformer(
            new FactoryExpressionTransformer((FactoryExpression<?>) projection.get(0)));
      }
    } else if (!forCount) {
      FactoryExpression<?> proj = FactoryExpressionUtils.wrap(projection);
      if (proj != null) {
        query.setResultTransformer(new FactoryExpressionTransformer(proj));
      }
    }
    return query;
  }
 @Override
 public void open() {
   currentRecordNumber = 0;
   Query hibernateQuery = session.createQuery(query);
   hibernateQuery.setReadOnly(true);
   if (maxResults >= 1) {
     hibernateQuery.setMaxResults(maxResults);
   }
   if (fetchSize >= 1) {
     hibernateQuery.setFetchSize(fetchSize);
   }
   scrollableResults = hibernateQuery.scroll(ScrollMode.FORWARD_ONLY);
 }
  /**
   * Creates the Hibernate query object.
   *
   * <p>If the value of the {@link
   * JRHibernateQueryExecuterFactory#PARAMETER_HIBERNATE_FILTER_COLLECTION
   * PARAMETER_HIBERNATE_FILTER_COLLECTION} is not null, then a filter query is created using the
   * value of the parameter as the collection.
   *
   * @param queryString the query string
   */
  protected synchronized void createQuery(String queryString) {
    if (log.isDebugEnabled()) {
      log.debug("HQL query: " + queryString);
    }

    Object filterCollection =
        getParameterValue(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_FILTER_COLLECTION);
    if (filterCollection == null) {
      query = session.createQuery(queryString);
    } else {
      query = session.createFilter(filterCollection, queryString);
    }
    query.setReadOnly(true);

    int fetchSize =
        JRProperties.getIntegerProperty(
            dataset, JRJdbcQueryExecuterFactory.PROPERTY_JDBC_FETCH_SIZE, 0);
    if (fetchSize != 0) {
      query.setFetchSize(fetchSize);
    }

    setParameters();
  }
Пример #6
0
 protected void applyReadOnly(boolean isReadOnly) {
   query.setReadOnly(isReadOnly);
 }