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:es.sm2.openppm.core.dao.TeamMemberDAO.java

License:Open Source License

/**
 * Search Team Members by Project.../*from ww  w .ja  v a  2  s. c o  m*/
 * 
 * @param project
 * @param since
 * @param until
 * @param showDisabled
  * @return
 */
public List<Teammember> consStaffinFtes(Project project, Date since, Date until, boolean showDisabled) {

    // Create query and restrictions
    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .add(Restrictions.or(Restrictions.eq(Teammember.STATUS, Constants.RESOURCE_ASSIGNED),
                    Restrictions.eq(Teammember.STATUS, Constants.RESOURCE_RELEASED)))
            .add(Restrictions.disjunction().add(Restrictions.between(Teammember.DATEIN, since, until))
                    .add(Restrictions.between(Teammember.DATEOUT, since, until))
                    .add(Restrictions.and(Restrictions.le(Teammember.DATEIN, since),
                            Restrictions.ge(Teammember.DATEOUT, until))));

    // Restriction project
    crit.createCriteria(Teammember.PROJECTACTIVITY).add(Restrictions.eq(Projectactivity.PROJECT, project));

    if (!showDisabled) {

        // Exclude employees and contacts disabled
        crit.createCriteria(Teammember.EMPLOYEE, "em").add(Restrictions.ne(Employee.DISABLE, true))
                .setFetchMode(Teammember.EMPLOYEE + "." + Employee.RESOURCEPROFILES, FetchMode.JOIN);

        crit.createCriteria("em." + Employee.CONTACT, "contact").add(Restrictions.ne(Contact.DISABLE, true));
    } else {
        crit.setFetchMode(Teammember.EMPLOYEE, FetchMode.JOIN);
        crit.setFetchMode(Teammember.EMPLOYEE + "." + Employee.RESOURCEPROFILES, FetchMode.JOIN);
        crit.setFetchMode(Teammember.EMPLOYEE + "." + Employee.CONTACT, FetchMode.JOIN);

        // Create aliases for order
        crit.createAlias(Teammember.EMPLOYEE, "em").createAlias("em." + Employee.CONTACT, "contact");
    }

    // Data needed
    crit.setFetchMode(Teammember.JOBCATEGORY, FetchMode.JOIN);

    // Order by name
    crit.addOrder(Order.asc("contact.fullName"));

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.TeamMemberDAO.java

License:Open Source License

/**
 * Search Team Members by Project with actuals FTEs
 * //w  ww .  j a v a  2s  .c o m
 * @param project
 * @param since
 * @param until
 * @return
 */
public List<Teammember> consStaffinActualsFtes(Project project, Date since, Date until) {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .add(Restrictions.eq(Teammember.STATUS, Constants.RESOURCE_ASSIGNED))
            .add(Restrictions.disjunction().add(Restrictions.between(Teammember.DATEIN, since, until))
                    .add(Restrictions.between(Teammember.DATEOUT, since, until))
                    .add(Restrictions.and(Restrictions.le(Teammember.DATEIN, since),
                            Restrictions.ge(Teammember.DATEOUT, until))));

    crit.createCriteria(Teammember.PROJECTACTIVITY).add(Restrictions.eq(Projectactivity.PROJECT, project));

    Criteria timesheetsCrit = crit.createCriteria(Teammember.EMPLOYEE).createCriteria(Employee.TIMESHEETS)
            .add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3))
            .add(Restrictions.disjunction().add(Restrictions.between(Timesheet.INITDATE, since, until))
                    .add(Restrictions.between(Timesheet.ENDDATE, since, until))
                    .add(Restrictions.and(Restrictions.le(Timesheet.INITDATE, since),
                            Restrictions.ge(Timesheet.ENDDATE, until))));

    timesheetsCrit.createCriteria(Timesheet.PROJECTACTIVITY)
            .add(Restrictions.eq(Projectactivity.PROJECT, project));

    crit.setFetchMode(Teammember.JOBCATEGORY, FetchMode.JOIN);
    crit.setFetchMode(Teammember.EMPLOYEE, FetchMode.JOIN);
    crit.setFetchMode(Teammember.EMPLOYEE + "." + Employee.CALENDARBASE, FetchMode.JOIN);

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.TeamMemberDAO.java

License:Open Source License

/**
 * Filter table utility//from www  .j a  va  2  s  .  c om
 * 
 * @param filter
 * @param joins
 * @param listResourcepool
 * @return
 * @throws ParseException
 */
public List<Teammember> filter(FiltroTabla filter, ArrayList<String> joins, List<Resourcepool> listResourcepool)
        throws ParseException {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setFirstResult(filter.getDisplayStart())
            .setMaxResults(filter.getDisplayLength());

    // Apply filters
    applyFilters(crit, filter.getFiltro(), filter.getOrden(), listResourcepool);

    // Add data needed
    if (joins != null) {
        for (String join : joins) {
            crit.setFetchMode(join, FetchMode.JOIN);
        }
    }

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.TeamMemberDAO.java

License:Open Source License

/**
 * Find team members filter by activity and status list
 *
 * @param activity/*from   w w  w. j av  a2  s.  c o m*/
 * @param statusList
 * @param joins
 * @return
 */
public List<Teammember> findByActivityAndStatus(Projectactivity activity, List<String> statusList,
        List<String> joins) {

    Criteria crit = getSession().createCriteria(getPersistentClass());

    if (joins != null) {
        for (String join : joins) {
            crit.setFetchMode(join, FetchMode.JOIN);
        }
    }

    crit.add(Restrictions.eq(Teammember.PROJECTACTIVITY, activity))
            .add(Restrictions.in(Teammember.STATUS, statusList));

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.TeamMemberDAO.java

License:Open Source License

/**
 * Find team members filter by activity, employee and dates
 * /*from w w w . j  a  v a 2s  .c  o  m*/
 * @param employee
 * @param projectactivity
 * @param dateIn
 * @param dateOut
 * @param joins
 * @return
 */
public List<Teammember> findByActivityAndDates(Employee employee, Projectactivity projectactivity, Date dateIn,
        Date dateOut, List<String> joins) {

    Criteria crit = getSession().createCriteria(getPersistentClass());

    if (joins != null) {
        for (String join : joins) {
            crit.setFetchMode(join, FetchMode.JOIN);
        }
    }

    crit.add(Restrictions.eq(Teammember.EMPLOYEE, employee))
            .add(Restrictions.eq(Teammember.PROJECTACTIVITY, projectactivity));

    // Filter by dates (since && until)
    if (dateIn != null && dateOut != null) {

        crit.add(Restrictions.disjunction().add(Restrictions.between(Teammember.DATEIN, dateIn, dateOut))
                .add(Restrictions.between(Teammember.DATEOUT, dateIn, dateOut))
                .add(Restrictions.and(Restrictions.le(Teammember.DATEIN, dateIn),
                        Restrictions.ge(Teammember.DATEOUT, dateOut))));
    } else if (dateIn != null) {
        crit.add(Restrictions.and(Restrictions.le(Teammember.DATEIN, dateIn),
                Restrictions.ge(Teammember.DATEOUT, dateIn)));
    } else if (dateOut != null) {
        crit.add(Restrictions.and(Restrictions.le(Teammember.DATEIN, dateOut),
                Restrictions.ge(Teammember.DATEOUT, dateOut)));
    }

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.TeamMemberDAO.java

License:Open Source License

/**
 * Search team members by filters/*  www .j  a v a  2 s.  c  o m*/
 * 
 * @param list
 * @param propertyOrder
 * @param typeOrder
 * @param joins 
 * @return
 */
public List<Teammember> findByFilters(ArrayList<PropertyRelation> list, String propertyOrder, String typeOrder,
        List<String> joins) {

    Criteria crit = getSession().createCriteria(getPersistentClass());

    if (joins != null) {
        for (String join : joins) {
            crit.setFetchMode(join, FetchMode.JOIN);
        }
    }

    for (PropertyRelation propertyRelation : list) {
        crit.add(findRestriction(propertyRelation));
    }

    addOrder(crit, propertyOrder, typeOrder);

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * Search timesheet by filters/*from w ww .j a  v a2 s  .  com*/
 * @param list
 * @param properyOrder
 * @param typeOrder
 * @param joins 
 * @return
 */
@SuppressWarnings("unchecked")
public List<Timesheet> findByFilters(ArrayList<PropertyRelation> list, String propertyOrder, String typeOrder,
        List<String> joins) {

    Criteria crit = getSession().createCriteria(getPersistentClass());

    if (joins != null) {
        for (String join : joins) {
            crit.setFetchMode(join, FetchMode.JOIN);
        }
    }

    for (PropertyRelation propertyRelation : list) {
        crit.add(findRestriction(propertyRelation));
    }

    addOrder(crit, propertyOrder, typeOrder);

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.WBSNodeDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Wbsnode> searchByCode(Wbsnode node) {
    Criteria crit = getSession().createCriteria(getPersistentClass());

    crit.setFetchMode(Wbsnode.PROJECTACTIVITIES, FetchMode.JOIN);
    crit.add(Example.create(node).excludeZeroes()).add(Restrictions.eq(Wbsnode.PROJECT, node.getProject()));

    return crit.list();
}

From source file:es.sm2.openppm.utils.hibernate.dao.AbstractGenericHibernateDAO.java

License:Open Source License

protected void addJoins(Criteria crit, List<String> joins) {
    if (joins != null) {
        for (String join : joins) {
            crit.setFetchMode(join, FetchMode.JOIN);
        }// w  ww.  j  av  a  2 s. c  om
    }
}

From source file:eu.jangos.manager.controller.AccountService.java

License:Apache License

/**
 * Provides access to the list of all accounts matching the parameters.
 *
 * The DateFilter works the same way: - NONE does not apply any filter. -
 * BEFORE & AFTER uses only the "From" date as filter. - BETWEEN uses both
 * "From" & "To" dates as filters.//from   w ww.  java2  s  . c  o m
 *
 * @param name The name of the accounts to be found. Can contain %.
 * @param createdFilter The filter type for the creation date.
 * @param createdFrom The first creation date filter.
 * @param createdTo The second creation date filter.
 * @param loginFilter The filter type for the login date.
 * @param loginFrom The first login date filter.
 * @param loginTo The second login date filter.
 * @param locked The filter type for the lock value.
 * @param banned The filter type for the ban value.
 * @param online The filter type for the online value.
 * @param locale The locale on which this search must filter.
 * @param realm The locale on which this search must filter.
 * @return A List of Accounts matching the criterias.
 */
public List<Account> getAllAccounts(String name, DateType createdFilter, Date createdFrom, Date createdTo,
        DateType loginFilter, Date loginFrom, Date loginTo, BooleanType locked, BooleanType banned,
        BooleanType online, Locale locale, Realm realm) {

    boolean error = false;
    String message = "You must enter the following parameters:\n";

    // Checking name input.
    if (name == null || name.isEmpty()) {
        logger.error("The parameter name is null or empty");
        message += "- A name\n";
        error = true;
    }

    // Checking dates input.
    if ((createdFilter != DateType.NONE && createdFrom == null)
            || (createdFilter == DateType.BETWEEN && createdTo == null)
            || (loginFilter != DateType.NONE && loginFrom == null)
            || (loginFilter == DateType.BETWEEN && loginTo == null)) {
        logger.error("A date filter has been requested while the date values are incorrect");
        message += "- Valid dates when selecting a date filter\n";
        error = true;
    }

    if (error) {
        throw new IllegalArgumentException(message);
    }

    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        Criteria query = session.createCriteria(Account.class, "acc");

        query.setFetchMode("locale", FetchMode.JOIN);
        query.setFetchMode("realm", FetchMode.JOIN);

        query.add(Restrictions.like("name", name));

        // This ban check is generating 2 SQL queries while, in SQL, you could do it in one.
        // Limitations of the criteria API.
        switch (banned) {
        case TRUE:
            // First, we get the list of IDs for the accounts that are banned.
            Criteria getBannedQuery = session.createCriteria(Account.class)
                    .setProjection(Projections.distinct(Projections.property("id")))
                    .createCriteria("bannedaccountsForFkBannedaccount", "ban", JoinType.LEFT_OUTER_JOIN)
                    .add(Restrictions.eq("ban.active", true));

            // Then we add these IDs to the query.
            query.add(Restrictions.in("id", getBannedQuery.list()));
            break;
        case FALSE:
            // First, we get the list of ID for the accounts that are not banned.
            Criteria getNotBanQuery = session.createCriteria(Account.class)
                    .setProjection(Projections.distinct(Projections.property("id")))
                    .createCriteria("bannedaccountsForFkBannedaccount", "ban", JoinType.LEFT_OUTER_JOIN)
                    .add(Restrictions.or(Restrictions.eq("ban.active", false),
                            Restrictions.isNull("ban.active")));

            // Then we add these IDs to the query.
            query.add(Restrictions.in("id", getNotBanQuery.list()));
            break;
        }

        // We add the realm restrictions, if it applies.
        if (!realm.getName().equals("ALL")) {
            query.add(Restrictions.eq("realm", realm));
        }

        // We add the locale restrictions, if it applies.
        if (!locale.getLocale().equals("ALL")) {
            query.add(Restrictions.eq("locale", locale));
        }

        query = Utils.applyDateFilter(query, "creation", createdFilter, createdFrom, createdTo);
        query = Utils.applyDateFilter(query, "lastlogin", loginFilter, loginFrom, loginTo);
        query = Utils.applyBooleanFilter(query, "locked", locked);
        query = Utils.applyBooleanFilter(query, "online", online);

        return query.list();
    } catch (HibernateException he) {
        logger.error("There was an error connecting to the database.");
        return null;
    }
}