public void testCachedQueryRegion() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Simple simple = new Simple();
    simple.setName("Simple 1");
    s.save(simple, new Long(10));
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    Query q = s.createQuery("from Simple s where s.name=?");
    q.setCacheRegion("foo");
    q.setCacheable(true);
    q.setString(0, "Simple 1");
    assertTrue(q.list().size() == 1);
    assertTrue(q.list().size() == 1);
    assertTrue(q.list().size() == 1);
    q = s.createQuery("from Simple s where s.name=:name");
    q.setCacheRegion("foo");
    q.setCacheable(true);
    q.setString("name", "Simple 1");
    assertTrue(q.list().size() == 1);
    simple = (Simple) q.list().get(0);

    q.setString("name", "Simple 2");
    assertTrue(q.list().size() == 0);
    assertTrue(q.list().size() == 0);
    simple.setName("Simple 2");
    assertTrue(q.list().size() == 1);
    assertTrue(q.list().size() == 1);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    s.update(simple, new Long(10));
    s.delete(simple);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    q = s.createQuery("from Simple s where s.name=?");
    q.setCacheRegion("foo");
    q.setCacheable(true);
    q.setString(0, "Simple 1");
    assertTrue(q.list().size() == 0);
    assertTrue(q.list().size() == 0);
    t.commit();
    s.close();
  }
  public int getTotalBalance(Object holder, boolean useRegion) throws Exception {
    List results = null;
    tm.begin();
    try {
      Query query =
          sessionFactory
              .getCurrentSession()
              .createQuery(
                  "select account.balance from Account as account where account.accountHolder = ?");
      query.setParameter(0, holder);
      if (useRegion) {
        query.setCacheRegion("AccountRegion");
      }
      query.setCacheable(true);
      results = query.list();
      tm.commit();
    } catch (Exception e) {
      log.error("rolling back", e);
      tm.rollback();
      throw e;
    }

    int total = 0;
    if (results != null) {
      for (Iterator it = results.iterator(); it.hasNext(); ) {
        total += ((Integer) it.next()).intValue();
        System.out.println("Total = " + total);
      }
    }
    return total;
  }
  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;
  }
Example #4
0
 public Object queryUnique(final String queryString, final Object... values) {
   Query query = getSession().createQuery(queryString);
   if (cacheRegion.get() != null) {
     query.setCacheRegion(cacheRegion.get());
     query.setCacheable(true);
     cacheRegion.set(null);
   }
   for (int position = 0; position < values.length; position++) {
     query.setParameter(position, values[position]);
   }
   return query.uniqueResult();
 }
Example #5
0
 public Object queryUnique(final String queryString, final Map paraMap) {
   Query query = getSession().createQuery(queryString);
   if (cacheRegion.get() != null) {
     query.setCacheRegion(cacheRegion.get());
     query.setCacheable(true);
     cacheRegion.set(null);
   }
   if (paraMap != null) {
     query.setProperties(paraMap);
   }
   return query.uniqueResult();
 }
 public int getCountForBranch(String branch, boolean useRegion) throws Exception {
   tm.begin();
   try {
     Query query =
         sessionFactory
             .getCurrentSession()
             .createQuery("select account from Account as account where account.branch = :branch");
     query.setString("branch", branch);
     if (useRegion) {
       query.setCacheRegion("AccountRegion");
     }
     query.setCacheable(true);
     int result = query.list().size();
     tm.commit();
     return result;
   } catch (Exception e) {
     log.error("rolling back", e);
     tm.rollback();
     throw e;
   }
 }
Example #7
0
 public List query(
     final String queryString,
     final Map paraMap,
     final Integer firstResult,
     final Integer maxResults) {
   Query query = getSession().createQuery(queryString);
   if (cacheRegion.get() != null) {
     query.setCacheRegion(cacheRegion.get());
     query.setCacheable(true);
     cacheRegion.set(null);
   }
   if (paraMap != null) {
     query.setProperties(paraMap);
   }
   if (firstResult != null) {
     query.setFirstResult(firstResult);
   }
   if (maxResults != null) {
     query.setMaxResults(maxResults);
   }
   return query.list();
 }
 public String getBranch(Object holder, boolean useRegion) throws Exception {
   tm.begin();
   try {
     Query query =
         sessionFactory
             .getCurrentSession()
             .createQuery(
                 "select account.branch from Account as account where account.accountHolder = ?");
     query.setParameter(0, holder);
     if (useRegion) {
       query.setCacheRegion("AccountRegion");
     }
     query.setCacheable(true);
     String result = (String) query.list().get(0);
     tm.commit();
     return result;
   } catch (Exception e) {
     log.error("rolling back", e);
     tm.rollback();
     throw e;
   }
 }
Example #9
0
 protected void applyCacheRegion(String regionName) {
   query.setCacheRegion(regionName);
 }