Package org.hibernate

Examples of org.hibernate.Criteria


     * @see org.andromda.timetracker.domain.TimecardDao#findByCriteria(org.andromda.timetracker.vo.TimecardSearchCriteriaVO)
     */
    protected java.util.List handleFindByCriteria(org.andromda.timetracker.vo.TimecardSearchCriteriaVO criteria)
    {
        // Create the timecard criteria
        Criteria timecardCriteria = this.getSession()
            .createCriteria(Timecard.class)
            .setFetchMode("submitter", FetchMode.JOIN)
            .setFetchMode("approver", FetchMode.JOIN);

        // Add submitter criteria
        if (criteria.getSubmitterId() != null) {
            timecardCriteria.createCriteria("submitter")
                .add(Restrictions.idEq(criteria.getSubmitterId()));
        }

        // Add approver criteria
        if (criteria.getApproverId() != null) {
            timecardCriteria.createCriteria("approver")
                .add(Restrictions.idEq(criteria.getApproverId()));
        }

        // Add status criteria
        if (criteria.getStatus() != null) {
            timecardCriteria.add(Restrictions.eq("status", criteria.getStatus()));
        }

        // Add startDateMin criteria
        if (criteria.getStartDateMin() != null) {
            timecardCriteria.add(Restrictions.ge("startDate", criteria.getStartDateMin()));
        }

        // Add startDateMax criteria
        if (criteria.getStartDateMax() != null) {
            timecardCriteria.add(Restrictions.le("startDate", criteria.getStartDateMax()));
        }

        java.util.List timecards = timecardCriteria.list();
        if (logger.isDebugEnabled()) {
            logger.debug(timecards.size() + " timecards found");
        }
        return timecards;
    }
View Full Code Here


   * you expect large results to be returned enable scrollableResultSet to retrieve only as many records as needed.
   */
  public Iterator<?> getElements(Filter filter, int offset, int maxResultSize) throws LayerException {
    try {
      Session session = getSessionFactory().getCurrentSession();
      Criteria criteria = session.createCriteria(getFeatureInfo().getDataSourceName());
      if (filter != null) {
        if (filter != Filter.INCLUDE) {
          CriteriaVisitor visitor = new CriteriaVisitor((HibernateFeatureModel) featureModel, dateFormat);
          Criterion c = (Criterion) filter.accept(visitor, criteria);
          if (c != null) {
            criteria.add(c);
          }
        }
      }

      // Sorting of elements.
      if (getFeatureInfo().getSortAttributeName() != null) {
        if (SortType.ASC.equals(getFeatureInfo().getSortType())) {
          criteria.addOrder(Order.asc(getFeatureInfo().getSortAttributeName()));
        } else {
          criteria.addOrder(Order.desc(getFeatureInfo().getSortAttributeName()));
        }
      }

      criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

      if (isScrollableResultSet()) {
        return (Iterator<?>) new ScrollIterator(criteria.scroll());
      } else {
        List<?> list = criteria.list();
        return list.iterator();
      }
    } catch (HibernateException he) {
      throw new HibernateLayerException(he, ExceptionCode.HIBERNATE_LOAD_FILTER_FAIL, getFeatureInfo()
          .getDataSourceName(), filter.toString());
View Full Code Here

      throw new HibernateLayerException(ExceptionCode.ATTRIBUTE_UNKNOWN, (Object) null);
    }
    AssociationAttributeInfo attributeInfo = getRecursiveAttributeInfo(attributeName, getFeatureInfo()
        .getAttributes());
    Session session = getSessionFactory().getCurrentSession();
    Criteria criteria = session.createCriteria(attributeInfo.getFeature().getDataSourceName());
    CriteriaVisitor visitor = new CriteriaVisitor((HibernateFeatureModel) getFeatureModel(), dateFormat);
    if (filter != null) {
      Criterion c = (Criterion) filter.accept(visitor, null);
      if (c != null) {
        criteria.add(c);
      }
    }
    List<Attribute<?>> attributes = new ArrayList<Attribute<?>>();
    for (Object object : criteria.list()) {
      try {
        attributes.add(converterService.toDto(object, attributeInfo));
      } catch (GeomajasException e) {
        throw new HibernateLayerException(e, ExceptionCode.HIBERNATE_ATTRIBUTE_TYPE_PROBLEM, attributeName);
      }
View Full Code Here

   *             oops
   */
  private Envelope getBoundsLocal(Filter filter) throws LayerException {
    try {
      Session session = getSessionFactory().getCurrentSession();
      Criteria criteria = session.createCriteria(getFeatureInfo().getDataSourceName());
      CriteriaVisitor visitor = new CriteriaVisitor((HibernateFeatureModel) getFeatureModel(), dateFormat);
      Criterion c = (Criterion) filter.accept(visitor, criteria);
      if (c != null) {
        criteria.add(c);
      }
      criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
      List<?> features = criteria.list();
      Envelope bounds = new Envelope();
      for (Object f : features) {
        Envelope geomBounds = getFeatureModel().getGeometry(f).getEnvelopeInternal();
        if (!geomBounds.isNull()) {
          bounds.expandToInclude(geomBounds);
View Full Code Here

      // the criteria API requires an alias for each join table !!!
      String prevAlias = null;
      for (int i = 0; i < props.length - 1; i++) {
        String alias = props[i] + "_alias";
        if (!aliases.contains(alias)) {
          Criteria criteria = (Criteria) userData;
          if (i == 0) {
            criteria.createAlias(props[0], alias);
          } else {
            criteria.createAlias(prevAlias + "." + props[i], alias);
          }
          aliases.add(alias);
        }
        prevAlias = alias;
      }
View Full Code Here



    public List<User> findByExample(User exampleUser, String orderByProperty, boolean orderDescending,
                                    int firstResult, int maxResults, String... ignoreProperty) {
        Criteria crit = prepareExampleCriteria(exampleUser, orderByProperty, orderDescending, ignoreProperty);
        crit.setFirstResult(firstResult).setMaxResults(maxResults);
        return (List<User>)crit.list();
    }
View Full Code Here

        return (List<User>)crit.list();
    }

    public int getRowCountByExample(User exampleUser, String... ignoreProperty) {

        Criteria crit = prepareExampleCriteria(exampleUser, null, false, ignoreProperty);
        ScrollableResults cursor = crit.scroll();
        cursor.last();
        int count = cursor.getRowNumber() + 1;
        cursor.close();
        return count;
    }
View Full Code Here

        for (String s : ignoreProperty) example.excludeProperty(s);

        Session session = (Session)entityManager.getDelegate();

        Criteria crit = session.createCriteria(User.class).add(example);
        if (orderByProperty != null)
                crit.addOrder( orderDescending ? Order.desc(orderByProperty) : Order.asc(orderByProperty) );

        return crit.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
    }
View Full Code Here

   * Remove all the entities of the given type from the datastore that have
   * one of these ids.
   */
  protected void _deleteById(Class<?> type, Serializable... ids) {
    type = metadataUtil.getUnproxiedClass(type); //Get the real entity class
    Criteria c = getSession().createCriteria(type);
    c.add(Restrictions.in("id", ids));
    for (Object entity : c.list()) {
      getSession().delete(entity);
    }
  }
View Full Code Here

   * <p>
   * <code>get()</code> always hits the database immediately.
   */
  protected <T> T[] _get(Class<T> type, Serializable... ids) {
    type = metadataUtil.getUnproxiedClass(type); //Get the real entity class
    Criteria c = getSession().createCriteria(type);
    c.add(Restrictions.in("id", ids));
    Object[] retVal = (Object[]) Array.newInstance(type, ids.length);

    for (Object entity : c.list()) {
      Serializable id = getMetadataUtil().getId(entity);
      for (int i = 0; i < ids.length; i++) {
        if (id.equals(ids[i])) {
          retVal[i] = entity;
          break;
View Full Code Here

TOP

Related Classes of org.hibernate.Criteria

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.