Exemplo n.º 1
0
  /**
   *
   *
   * <pre>
   * call hql unique result (returns one or null)
   *
   * e.g.
   *
   * Hib3GroupDAO hib3GroupDAO = HibernateSession.byHqlStatic()
   * .createQuery("from Hib3GroupDAO as g where g.uuid = :uuid")
   *  .setCacheable(false)
   *  .setCacheRegion(KLASS + ".Exists")
   *  .setString("uuid", uuid).uniqueResult(Hib3GroupDAO.class);
   *
   * </pre>
   *
   * @param returnType type of the result (in future can use this for typecasting)
   * @param theCriterions are the criterions to use (pack multiple with HibUtils.listCrit())
   * @param <T> is the template
   * @return the object or null if none found
   * @throws GrouperDAOException
   */
  public <T> T uniqueResult(Class<T> returnType, Criterion theCriterions)
      throws GrouperDAOException {
    this.persistentClass = returnType;
    this.criterions = theCriterions;
    try {
      GrouperTransactionType grouperTransactionTypeToUse =
          (GrouperTransactionType)
              ObjectUtils.defaultIfNull(
                  this.grouperTransactionType, GrouperTransactionType.READONLY_OR_USE_EXISTING);

      T result =
          (T)
              HibernateSession.callbackHibernateSession(
                  grouperTransactionTypeToUse,
                  AuditControl.WILL_NOT_AUDIT,
                  new HibernateHandler() {

                    public Object callback(HibernateHandlerBean hibernateHandlerBean)
                        throws GrouperDAOException {
                      HibernateSession hibernateSession =
                          hibernateHandlerBean.getHibernateSession();

                      Session session = hibernateSession.getSession();
                      Criteria criteria = ByCriteriaStatic.this.attachCriteriaInfo(session);
                      GrouperContext.incrementQueryCount();
                      Object object = criteria.uniqueResult();
                      HibUtils.evict(hibernateSession, object, true);
                      return object;
                    }
                  });

      return result;
    } catch (GrouperStaleObjectStateException e) {
      throw e;
    } catch (RuntimeException e) {

      String errorString = "Exception in uniqueResult: (" + returnType + "), " + this;

      if (!GrouperUtil.injectInException(e, errorString)) {
        LOG.error(errorString, e);
      }

      throw e;
    }
  }
Exemplo n.º 2
0
  /**
   *
   *
   * <pre>
   * call hql unique result (returns one or null)
   *
   * e.g.
   *
   * List<Hib3GroupTypeTupleDAO> hib3GroupTypeTupleDAOs =
   *  HibernateSession.byHqlStatic()
   *    .createQuery("from Hib3GroupTypeTupleDAO as gtt where gtt.groupUuid = :group")
   *    .setCacheable(false).setString("group", uuid).list(Hib3GroupTypeTupleDAO.class);
   * </pre>
   *
   * @param returnType type of the result (can typecast)
   * @param theCriterions are the criterions to use (pack multiple with HibUtils.listCrit())
   * @param <T> is the template
   * @return the list or the empty list if not found (never null)
   * @throws GrouperDAOException
   */
  public <T> List<T> list(Class<T> returnType, Criterion theCriterions) throws GrouperDAOException {
    this.persistentClass = returnType;
    this.criterions = theCriterions;
    try {
      GrouperTransactionType grouperTransactionTypeToUse =
          (GrouperTransactionType)
              ObjectUtils.defaultIfNull(
                  this.grouperTransactionType, GrouperTransactionType.READONLY_OR_USE_EXISTING);

      List<T> result =
          (List<T>)
              HibernateSession.callbackHibernateSession(
                  grouperTransactionTypeToUse,
                  AuditControl.WILL_NOT_AUDIT,
                  new HibernateHandler() {

                    public Object callback(HibernateHandlerBean hibernateHandlerBean)
                        throws GrouperDAOException {
                      HibernateSession hibernateSession =
                          hibernateHandlerBean.getHibernateSession();

                      Session session = hibernateSession.getSession();
                      List<T> list = null;

                      // see if we are even retrieving the results
                      if (ByCriteriaStatic.this.queryOptions == null
                          || ByCriteriaStatic.this.queryOptions.isRetrieveResults()) {
                        Criteria criteria = ByCriteriaStatic.this.attachCriteriaInfo(session);
                        GrouperContext.incrementQueryCount();
                        // not sure this can ever be null, but make sure not to make iterating
                        // results easier
                        list = GrouperUtil.nonNull(criteria.list());
                        HibUtils.evict(hibernateSession, list, true);
                      }
                      // no nulls
                      list = GrouperUtil.nonNull(list);
                      QueryPaging queryPaging =
                          ByCriteriaStatic.this.queryOptions == null
                              ? null
                              : ByCriteriaStatic.this.queryOptions.getQueryPaging();

                      // now see if we should get the query count
                      boolean retrieveQueryCountNotForPaging =
                          ByCriteriaStatic.this.queryOptions != null
                              && ByCriteriaStatic.this.queryOptions.isRetrieveCount();
                      boolean findQueryCount =
                          (queryPaging != null && queryPaging.isDoTotalCount())
                              || (retrieveQueryCountNotForPaging);
                      if (findQueryCount) {

                        long resultSize = -1;
                        if (queryPaging != null) {
                          // see if we already know the total size (if less than page size and first
                          // page)
                          resultSize = GrouperUtil.length(list);
                          if (resultSize >= queryPaging.getPageSize()) {
                            resultSize = -1;
                          } else {
                            // we are on the last page, see how many records came before us, add
                            // those in
                            resultSize +=
                                (queryPaging.getPageSize() * (queryPaging.getPageNumber() - 1));
                          }
                        }

                        // do this if we dont have a total, or if we are not caching the total
                        if ((queryPaging != null
                                && (queryPaging.getTotalRecordCount() < 0
                                    || !queryPaging.isCacheTotalCount()))
                            || resultSize > -1
                            || retrieveQueryCountNotForPaging) {

                          // if we dont already know the size
                          if (resultSize == -1) {
                            queryCountQueries++;

                            Criteria countQuery =
                                StringUtils.isBlank(ByCriteriaStatic.this.alias)
                                    ? session.createCriteria(ByCriteriaStatic.this.persistentClass)
                                    : session.createCriteria(ByCriteriaStatic.this.alias);

                            // turn it into a row count
                            countQuery.setProjection(
                                Projections.projectionList().add(Projections.rowCount()));

                            // add criterions
                            if (ByCriteriaStatic.this.criterions != null) {
                              countQuery.add(ByCriteriaStatic.this.criterions);
                            }
                            resultSize = (Long) countQuery.list().get(0);
                          }

                          if (queryPaging != null) {
                            queryPaging.setTotalRecordCount((int) resultSize);

                            // calculate the page stuff like how many pages etc
                            queryPaging.calculateIndexes();
                          }
                          if (retrieveQueryCountNotForPaging) {
                            ByCriteriaStatic.this.queryOptions.setCount(resultSize);
                          }
                        }
                      }

                      return list;
                    }
                  });

      return result;
    } catch (GrouperStaleObjectStateException e) {
      throw e;
    } catch (GrouperDAOException e) {
      GrouperUtil.injectInException(e, "Exception in list: (" + returnType + "), " + this);
      throw e;
    } catch (RuntimeException e) {
      GrouperUtil.injectInException(e, "Exception in list: (" + returnType + "), " + this);
      throw e;
    }
  }
Exemplo n.º 3
0
 public Session getSession() {
   return HibernateSession.getSession(sessionFactory);
 }
Exemplo n.º 4
0
 public void end(Object identity) {
   HibernateSession.closeSession(sessionFactory, identity);
 }
Exemplo n.º 5
0
 public Object start() {
   return HibernateSession.createSession(getSessionFactory());
 }
Exemplo n.º 6
0
 public void startTxManaged() throws HibernateException {
   HibernateSession.createTxManagedSession(sessionFactory);
 }