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:eu.jangos.manager.controller.RealmService.java

License:Apache License

/**
 * Return the list of all realms matching the given criterias.
 *
 * @param name The name of the realm to be found.
 * @param type The type of realm to be found.
 * @param zone The zone of realm to be found.
 * @return A list of realm corresponding to the given filters.
 *//*from ww  w . ja v a2 s  . c o m*/
public List<Realm> getAllRealms(String name, Realmtype type, Realmtimezone zone) {
    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        Criteria query = session.createCriteria(Realm.class);

        query.setFetchMode("realmtimezone", FetchMode.JOIN);
        query.setFetchMode("realmtype", FetchMode.JOIN);

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

        if (!type.getType().equals("ALL")) {
            query.add(Restrictions.eq("realmtype", type));
        }

        if (!zone.getName().equals("ALL")) {
            query.add(Restrictions.eq("realmtimezone", zone));
        }

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

From source file:fr.mael.microrss.dao.impl.UserArticleDaoImpl.java

License:Open Source License

public List<UserArticle> search(String queryStr, User user, int start, int nb)
        throws ParseException, IOException, InvalidTokenOffsetsException {
    FullTextSession searchSession = Search.getFullTextSession(getSessionFactory().getCurrentSession());
    QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_31,
            new String[] { "article.content", "article.title" }, new StandardAnalyzer(Version.LUCENE_31));
    org.apache.lucene.search.Query query = parser.parse(queryStr);
    FullTextQuery hibQuery = searchSession.createFullTextQuery(query, UserArticle.class);
    Criteria fetchingStrategy = searchSession.createCriteria(UserArticle.class);
    fetchingStrategy.setFetchMode("article.feed", FetchMode.JOIN);
    fetchingStrategy.setFetchMode("userLabels", FetchMode.JOIN);
    fetchingStrategy.add(Property.forName("user").eq(user));
    hibQuery.setCriteriaQuery(fetchingStrategy);
    hibQuery.setFirstResult(start);/*from   www .  j  a  v a  2s  .  c  om*/
    hibQuery.setMaxResults(nb);

    QueryScorer scorer = new QueryScorer(query);
    SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("[highlight]", "[/highlight]");
    Highlighter highlighter = new Highlighter(formatter, scorer);
    highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer, 200));

    List<UserArticle> userArticles = (List<UserArticle>) hibQuery.list();

    for (UserArticle userArticle : userArticles) {
        String highlight = highlighter.getBestFragment(new StandardAnalyzer(Version.LUCENE_32), "content",
                userArticle.getArticle().getContent());
        if (highlight != null) {
            highlight = highlight.replaceAll("\\<.*?>", "").replace("\n", " ");
            userArticle.getArticle().setHighlight(highlight);
        }
    }

    return userArticles;
}

From source file:fsl.ta.toms.roms.dao.impl.ReportDAOImpl.java

private Integer absentPersonCount(Integer roadOperationId) {
    /* Get Count of Absent Persons */
    Criteria criteriaAssignedPersons = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(AssignedPersonDO.class, "a");

    List<Integer> teamIds = getTeamIdsForRoadOp(roadOperationId);

    if (teamIds != null && teamIds.size() > 0)
        criteriaAssignedPersons.add(Restrictions.in("a.assignedPersonKey.team.teamId", teamIds));

    criteriaAssignedPersons.add(Restrictions.eq("a.attended", "n").ignoreCase());
    criteriaAssignedPersons.setProjection(Projections.count("a.attended"));
    criteriaAssignedPersons.setFetchMode("a", FetchMode.LAZY);
    Iterator iterator = criteriaAssignedPersons.list().iterator();

    Integer AbsentMembersCount = (Integer) iterator.next();

    criteriaAssignedPersons.setFlushMode(FlushMode.ALWAYS);
    return AbsentMembersCount;
}

From source file:fsl.ta.toms.roms.dao.impl.ReportDAOImpl.java

private Integer absentPersonTeamCount(Integer teamId) {
    /* Get Count of Absent Persons */
    Criteria criteriaAssignedPersons = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(AssignedPersonDO.class, "a");
    criteriaAssignedPersons.add(Restrictions.eq("a.assignedPersonKey.team.teamId", teamId));
    criteriaAssignedPersons.add(Restrictions.eq("a.attended", "n").ignoreCase());
    criteriaAssignedPersons.setProjection(Projections.count("a.attended"));
    criteriaAssignedPersons.setFetchMode("a", FetchMode.LAZY);
    Iterator iterator = criteriaAssignedPersons.list().iterator();

    Integer AbsentMembersCount = (Integer) iterator.next();

    criteriaAssignedPersons.setFlushMode(FlushMode.ALWAYS);
    return AbsentMembersCount;
}

From source file:fsl.ta.toms.roms.dao.impl.ReportDAOImpl.java

private Integer teamMemberPersonCount(Integer roadOperationId) {
    /* Get Count of Absent Persons */
    Criteria criteriaAssignedPersons = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(AssignedPersonDO.class, "a");

    List<Integer> teamIds = this.getTeamIdsForRoadOp(roadOperationId);

    if (teamIds != null && teamIds.size() > 0)
        criteriaAssignedPersons.add(Restrictions.in("a.assignedPersonKey.team.teamId", teamIds));

    criteriaAssignedPersons.setProjection(Projections.rowCount());
    criteriaAssignedPersons.setFetchMode("a", FetchMode.LAZY);
    Iterator iterator = criteriaAssignedPersons.list().iterator();

    Integer AbsentMembersCount = (Integer) iterator.next();

    criteriaAssignedPersons.setFlushMode(FlushMode.ALWAYS);
    return AbsentMembersCount;
}

From source file:fsl.ta.toms.roms.dao.impl.ReportDAOImpl.java

private Integer teamMemberPersonTeamCount(Integer teamId) {
    /* Get Count of Absent Persons */
    Criteria criteriaAssignedPersons = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(AssignedPersonDO.class, "a");
    criteriaAssignedPersons.add(Restrictions.eq("a.assignedPersonKey.team.teamId", teamId));

    criteriaAssignedPersons.setProjection(Projections.rowCount());
    criteriaAssignedPersons.setFetchMode("a", FetchMode.LAZY);
    Iterator iterator = criteriaAssignedPersons.list().iterator();

    Integer AbsentMembersCount = (Integer) iterator.next();

    criteriaAssignedPersons.setFlushMode(FlushMode.ALWAYS);
    return AbsentMembersCount;
}

From source file:fsl.ta.toms.roms.dao.impl.ReportDAOImpl.java

private Integer summonsTeamCount(Integer roadOperationId, Integer teamId) {
    /* Get Count of Summons For Road Operation */
    Criteria criteriaSummons = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(SummonsDO.class, "s");

    criteriaSummons.createAlias("s.roadCheckOffenceOutcome", "rout", Criteria.LEFT_JOIN);
    criteriaSummons.createAlias("rout.roadCheckOffence", "roff", Criteria.LEFT_JOIN);
    criteriaSummons.createAlias("roff.roadCheck", "rchk", Criteria.LEFT_JOIN);
    criteriaSummons.createAlias("rchk.compliance", "comp", Criteria.LEFT_JOIN);
    criteriaSummons.createAlias("comp.roadOperation", "rop", Criteria.LEFT_JOIN);

    criteriaSummons.add(Restrictions.eq("rop.roadOperationId", roadOperationId));

    /* Get list of ta staff ids which are on a team. */
    Criteria criteria = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(SummonsDO.class, "team");

    if (this.getStaffIdForTeam(teamId) != null) {
        criteriaSummons.add(Restrictions.in("s.taStaff.staffId", this.getStaffIdForTeam(teamId)));

        criteriaSummons.setProjection(Projections.rowCount());

        criteriaSummons.setFetchMode("s", FetchMode.LAZY);
        criteriaSummons.setFlushMode(FlushMode.ALWAYS);

        Iterator iterator = criteriaSummons.list().iterator();

        Integer summonsCount = (Integer) iterator.next();

        return summonsCount;
    } else//  ww  w . j a  va  2  s  .c  om
        return 0;
}

From source file:fsl.ta.toms.roms.dao.impl.ReportDAOImpl.java

private Integer warningNoticeCount(Integer roadOperationId) {
    /* Get Count of Summons For Road Operation */
    Criteria criteriaWarningNotice = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(WarningNoticeDO.class, "w");

    criteriaWarningNotice.createAlias("w.roadCheckOffenceOutcome", "rout", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("rout.roadCheckOffence", "roff", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("roff.roadCheck", "rchk", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("rchk.compliance", "comp", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("comp.roadOperation", "rop", Criteria.LEFT_JOIN);

    criteriaWarningNotice.add(Restrictions.eq("rop.roadOperationId", roadOperationId));

    criteriaWarningNotice.setProjection(Projections.rowCount());

    criteriaWarningNotice.setFetchMode("w", FetchMode.LAZY);
    criteriaWarningNotice.setFlushMode(FlushMode.ALWAYS);
    Iterator iterator = criteriaWarningNotice.list().iterator();

    Integer warningNoticeCount = (Integer) iterator.next();

    return warningNoticeCount;
}

From source file:fsl.ta.toms.roms.dao.impl.ReportDAOImpl.java

private Integer warningNoticeTeamCount(Integer roadOperationId, Integer teamId) {
    /* Get Count of Summons For Road Operation */
    Criteria criteriaWarningNotice = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(WarningNoticeDO.class, "w");

    criteriaWarningNotice.createAlias("w.roadCheckOffenceOutcome", "rout", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("rout.roadCheckOffence", "roff", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("roff.roadCheck", "rchk", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("rchk.compliance", "comp", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("comp.roadOperation", "rop", Criteria.LEFT_JOIN);

    criteriaWarningNotice.add(Restrictions.eq("rop.roadOperationId", roadOperationId));

    if (this.getStaffIdForTeam(teamId) != null) {
        criteriaWarningNotice.add(Restrictions.in("w.taStaff.staffId", this.getStaffIdForTeam(teamId)));

        criteriaWarningNotice.setProjection(Projections.rowCount());

        criteriaWarningNotice.setFetchMode("w", FetchMode.LAZY);
        criteriaWarningNotice.setFlushMode(FlushMode.ALWAYS);
        Iterator iterator = criteriaWarningNotice.list().iterator();

        Integer warningNoticeCount = (Integer) iterator.next();

        return warningNoticeCount;
    } else// ww  w. j  a v  a2  s  . c o  m
        return 0;
}

From source file:fsl.ta.toms.roms.dao.impl.ReportDAOImpl.java

private Integer warningNoticeCount(Integer roadOperationId, Integer personId, String personType) {
    /* Get Count of Summons For Road Operation */
    Criteria criteriaWarningNotice = this.hibernateTemplate.getSessionFactory().getCurrentSession()
            .createCriteria(WarningNoticeDO.class, "w");

    criteriaWarningNotice.createAlias("w.roadCheckOffenceOutcome", "rout", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("rout.roadCheckOffence", "roff", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("roff.roadCheck", "rchk", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("rchk.compliance", "comp", Criteria.LEFT_JOIN);
    criteriaWarningNotice.createAlias("comp.roadOperation", "rop", Criteria.LEFT_JOIN);

    criteriaWarningNotice.add(Restrictions.eq("rop.roadOperationId", roadOperationId));

    if (personType.equalsIgnoreCase(Constants.PersonType.TA_STAFF)) {
        criteriaWarningNotice.createAlias("w.taStaff", "ta");
        criteriaWarningNotice.createAlias("ta.person", "p");
    } else {//from  w ww  .  j  a v a2s  .c o m
        return -1;
    }

    criteriaWarningNotice.add(Restrictions.eq("p.personId", personId));

    criteriaWarningNotice.setProjection(Projections.rowCount());

    criteriaWarningNotice.setFetchMode("w", FetchMode.LAZY);
    criteriaWarningNotice.setFlushMode(FlushMode.ALWAYS);
    Iterator iterator = criteriaWarningNotice.list().iterator();

    Integer warningNoticeCount = (Integer) iterator.next();

    return warningNoticeCount;
}