Example usage for org.hibernate Criteria setFetchMode

List of usage examples for org.hibernate Criteria setFetchMode

Introduction

In this page you can find the example usage for org.hibernate Criteria setFetchMode.

Prototype

public Criteria setFetchMode(String associationPath, FetchMode mode) throws HibernateException;

Source Link

Document

Specify an association fetching strategy for an association or a collection of values.

Usage

From source file:net.paulgray.mockrest.course.MockCourseService.java

public List getCoursesForUser(User user, String courseFilter) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(MockUser.class);
    crit.add(Restrictions.eq("id", user.getId()));
    crit.setFetchMode("courses", FetchMode.JOIN);
    MockUser mockUser = (MockUser) crit.uniqueResult();
    return mockUser.getCourses();
}

From source file:net.sf.xplanner.dao.impl.TaskDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w  w w.  j  a va 2  s  .  c o  m*/
@Transactional(readOnly = true)
public List<Task> getCurrentTasksForPerson(final int personId) {
    final Criteria criteria = this.createCriteria();
    criteria.createAlias("userStory", "userStory");
    criteria.createAlias("userStory.iteration", "iteration");
    final Date now = new Date();
    criteria.add(Restrictions.le("iteration.startDate", now));
    criteria.add(Restrictions.ge("iteration.endDate", now));
    criteria.add(Restrictions.eq("acceptorId", personId));
    criteria.setFetchMode("timeEntries", FetchMode.SELECT);
    return criteria.list();
}

From source file:net.sf.xplanner.dao.impl.UserStoryDaoImpl.java

License:Open Source License

@Override
@Transactional()/*  w ww .j  a va  2s. c  o m*/
public List<UserStory> getStoriesForPersonWhereCustomer(final int personId) {
    final Criteria criteria = this.createCriteria();
    criteria.createAlias("customer", "customer");
    criteria.createAlias("iteration", "iteration");
    criteria.add(Restrictions.eq("customer.id", personId));
    criteria.add(Restrictions.gt("iteration.endDate", new Date()));
    criteria.setFetchMode("tasks", FetchMode.SELECT);
    criteria.setFetchMode("tasks.timeEntries", FetchMode.SELECT);
    return criteria.list();
}

From source file:net.sf.xplanner.dao.impl.UserStoryDaoImpl.java

License:Open Source License

@Override
@Transactional()//from  ww w  .java 2 s  . c  o m
public List<UserStory> getStoriesForPersonWhereTracker(final int personId) {
    final Criteria criteria = this.createCriteria();
    criteria.createAlias("iteration", "iteration");
    criteria.add(Restrictions.eq("trackerId", personId));
    criteria.add(Restrictions.gt("iteration.endDate", new Date()));
    criteria.setFetchMode("tasks", FetchMode.SELECT);
    criteria.setFetchMode("tasks.timeEntries", FetchMode.SELECT);
    return criteria.list();
}

From source file:net.webpasswordsafe.server.dao.PasswordDAOHibernate.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public List<Password> findPasswordByFuzzySearch(String query, User user, boolean activeOnly,
        Collection<Tag> tags, Match tagMatch) {
    //kludge to not use ilike on text column if MSSQL
    boolean isMSSQL = ((SessionFactoryImpl) getSessionFactory()).getDialect().toString().contains("SQLServer");
    Criteria crit = getSession().createCriteria(getPersistentClass());
    crit.setFetchMode("tags", FetchMode.JOIN);
    crit.add(Restrictions.or(/*from   w w  w.ja  v  a  2s . c  om*/
            Restrictions.or(Restrictions.ilike("name", query, MatchMode.ANYWHERE),
                    Restrictions.ilike("username", query, MatchMode.ANYWHERE)),
            isMSSQL ? Restrictions.like("notes", query, MatchMode.ANYWHERE)
                    : Restrictions.ilike("notes", query, MatchMode.ANYWHERE)));
    if (activeOnly) {
        crit.add(Restrictions.eq("active", true));
    }
    Criterion tagsCriterion = null;
    for (Tag tag : tags) {
        Criterion tc = Restrictions.sqlRestriction(
                "? in (select tag_id from password_tags where password_id = {alias}.id)", tag.getId(),
                StandardBasicTypes.LONG);
        if (null == tagsCriterion) {
            tagsCriterion = tc;
        } else {
            tagsCriterion = tagMatch.equals(Match.AND) ? Restrictions.and(tagsCriterion, tc)
                    : Restrictions.or(tagsCriterion, tc);
        }
    }
    if (tagsCriterion != null)
        crit.add(tagsCriterion);
    crit.createAlias("permissions", "pm");
    crit.add(Restrictions.in("pm.accessLevel",
            new String[] { AccessLevel.READ.name(), AccessLevel.WRITE.name(), AccessLevel.GRANT.name() }));
    if (!authorizer.isAuthorized(user, Function.BYPASS_PASSWORD_PERMISSIONS.name())) {
        DetachedCriteria groupQuery = DetachedCriteria.forClass(Group.class);
        groupQuery.setProjection(Projections.id());
        groupQuery.createCriteria("users", "u").add(Restrictions.eq("u.id", user.getId()));
        crit.add(Restrictions.or(Restrictions.eq("pm.subject", user),
                Subqueries.propertyIn("pm.subject", groupQuery)));
    }
    crit.addOrder(Order.asc("name"));
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return crit.list();
}

From source file:ome.services.search.SearchAction.java

License:Open Source License

AnnotationCriteria(Criteria base, List<Class> fetchAnnotations) {
    this.base = base;
    this.fetchAnnotations = fetchAnnotations;
    if (fetchAnnotations.size() > 0) {
        joinType = Criteria.LEFT_JOIN;// w w w.  j a  v  a 2 s.  co  m
        base.setFetchMode("annotationLinks", FetchMode.JOIN);
        getLinks().setFetchMode("child", FetchMode.JOIN);
    } else {
        joinType = Criteria.INNER_JOIN;
    }
}

From source file:org.accesointeligente.server.robots.RequestCreator.java

License:Open Source License

public void createRequests() {
    Session hibernate = null;/*w ww. j a v  a 2  s.co m*/

    try {
        hibernate = HibernateUtil.getSession();
        hibernate.beginTransaction();
        Criteria criteria = hibernate.createCriteria(Request.class);
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.setFetchMode("institution", FetchMode.JOIN);
        criteria.add(Restrictions.eq("status", RequestStatus.NEW));
        criteria.createAlias("institution", "ins").add(Restrictions.eq("ins.masterEnabled", true));
        List<Request> newRequests = criteria.list();
        hibernate.getTransaction().commit();

        for (Request request : newRequests) {
            if (!request.getInstitution().getEnabled()) {
                continue;
            }

            logger.info("requestId = " + request.getId());

            try {
                Robot robot = RobotContext.getRobot(request.getInstitution().getInstitutionClass());

                if (robot != null) {
                    request = robot.makeRequest(request);
                    hibernate = HibernateUtil.getSession();
                    hibernate.beginTransaction();
                    hibernate.update(request);
                    hibernate.getTransaction().commit();
                }
            } catch (Exception ex) {
                logger.error("requestId = " + request.getId(), ex);
            }
        }
    } catch (Exception ex) {
        if (hibernate != null && hibernate.isOpen() && hibernate.getTransaction().isActive()) {
            hibernate.getTransaction().rollback();
            logger.error("Failure", ex);
        }
    }
}

From source file:org.accesointeligente.server.robots.RequestUpdater.java

License:Open Source License

public void updateRequests() {
    Session hibernate = null;/*from ww w .  ja  va2s .  c  om*/

    try {
        hibernate = HibernateUtil.getSession();
        hibernate.beginTransaction();
        Criteria criteria = hibernate.createCriteria(Request.class);
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.setFetchMode("institution", FetchMode.JOIN);
        criteria.add(Restrictions.eq("status", RequestStatus.ERROR));
        criteria.createAlias("institution", "ins").add(Restrictions.eq("ins.masterEnabled", true));
        List<Request> newRequests = criteria.list();
        hibernate.getTransaction().commit();

        for (Request request : newRequests) {
        }
    } catch (Exception ex) {
        if (hibernate != null && hibernate.isOpen() && hibernate.getTransaction().isActive()) {
            hibernate.getTransaction().rollback();
            logger.error("Failure", ex);
        }
    }
}

From source file:org.andromda.timetracker.domain.TimecardDaoImpl.java

/**
 * @see TimecardDao#findByCriteria(TimecardSearchCriteriaVO)
 *///ww  w . ja  v a  2  s  . c  o m
@Override
protected List handleFindByCriteria(TimecardSearchCriteriaVO criteria) {
    // Create the timecard criteria
    Criteria timecardCriteria = this.hibernateSession.createCriteria(Timecard.class);
    timecardCriteria.setFetchMode("submitter", FetchMode.JOIN);
    timecardCriteria.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()));
    }

    List timecards = timecardCriteria.list();
    if (this.logger.isDebugEnabled()) {
        this.logger.debug(timecards.size() + " timecards found");
    }
    return timecards;
}

From source file:org.apache.ode.daohib.bpel.BpelDAOConnectionImpl.java

License:Apache License

@SuppressWarnings({ "unchecked", "deprecation" })
public List<Date> bpelEventTimelineQuery(InstanceFilter ifilter, BpelEventFilter efilter) {
    CriteriaBuilder cb = new CriteriaBuilder();
    Criteria crit = getSession().createCriteria(HBpelEvent.class);
    if (ifilter != null)
        cb.buildCriteria(crit, efilter);
    if (ifilter != null)
        cb.buildCriteria(crit.createCriteria("instance"), ifilter);
    crit.setFetchMode("tstamp", FetchMode.EAGER);
    crit.setProjection(Projections.property("tstamp"));
    return crit.list();
}