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.longfalcon.newsj.persistence.hibernate.ReleaseCommentDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public List<ReleaseComment> getReleaseCommentsByUser(long userId, int start, int pageSize) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ReleaseComment.class);
    criteria.add(Restrictions.eq("user.id", userId));
    criteria.setFirstResult(start).setMaxResults(pageSize);
    criteria.setFetchMode("user", FetchMode.JOIN);
    criteria.addOrder(Order.desc("createDate"));

    return criteria.list();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseCommentDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public long countReleaseCommentsByUser(long userId) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ReleaseComment.class);
    criteria.add(Restrictions.eq("user.id", userId));
    criteria.setFetchMode("user", FetchMode.JOIN);
    criteria.setProjection(Projections.rowCount());

    return (Long) criteria.uniqueResult();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public List<Release> findByCategoriesMaxAgeAndGroup(Collection<Integer> categoryIds, Date maxAge,
        Collection<Integer> excludedCategoryIds, Long groupId, String orderByField, boolean descending,
        int offset, int pageSize) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);
    if (!categoryIds.isEmpty()) {
        criteria.add(Restrictions.in("category.id", categoryIds));
    }/*w w w.ja  v a 2s . c  o  m*/
    if (maxAge != null) {
        criteria.add(Restrictions.gt("postDate", maxAge));
    }
    if (excludedCategoryIds != null && !excludedCategoryIds.isEmpty()) {
        criteria.add(Restrictions.not(Restrictions.in("category.id", excludedCategoryIds)));
    }
    if (groupId != null) {
        criteria.add(Restrictions.eq("groupId", groupId));
    }
    if (descending) {
        criteria.addOrder(Order.desc(orderByField));
    } else {
        criteria.addOrder(Order.asc(orderByField));
    }
    criteria.setFetchMode("category", FetchMode.JOIN);
    criteria.setFirstResult(offset).setMaxResults(pageSize);

    return criteria.list();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Release findByGuid(String guid) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);
    criteria.add(Restrictions.eq("guid", guid));
    criteria.setFetchMode("category", FetchMode.JOIN);

    return (Release) criteria.uniqueResult();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Release findByReleaseId(long releaseId) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);
    criteria.add(Restrictions.eq("id", releaseId));
    criteria.setFetchMode("category", FetchMode.JOIN);

    return (Release) criteria.uniqueResult();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public List<Release> findReleasesBeforeDate(Date before) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);
    criteria.add(Restrictions.lt("postDate", before));
    criteria.setFetchMode("category", FetchMode.JOIN);

    return criteria.list();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public List<Release> findReleasesByNameAndDateRange(String relName, Date startDate, Date endDate) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);
    criteria.add(Restrictions.eq("searchName", relName));
    criteria.add(Restrictions.between("postDate", startDate, endDate));
    criteria.setFetchMode("category", FetchMode.JOIN);

    return criteria.list();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public List<Release> getReleases(int offset, int pageSize) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);
    criteria.setFirstResult(offset).setMaxResults(pageSize);
    criteria.setFetchMode("category", FetchMode.JOIN);

    return criteria.list();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public List<Release> searchByCategoriesMaxAgeAndGroup(String[] searchTokens, Collection<Integer> categoryIds,
        Date maxAge, Collection<Integer> excludedCategoryIds, Long groupId, String orderByField,
        boolean descending, int offset, int pageSize) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);

    addSearchTokens(searchTokens, criteria);

    if (!categoryIds.isEmpty()) {
        criteria.add(Restrictions.in("category.id", categoryIds));
    }//w  w  w  . j a va  2s.  com
    if (maxAge != null) {
        criteria.add(Restrictions.gt("postDate", maxAge));
    }
    if (excludedCategoryIds != null && !excludedCategoryIds.isEmpty()) {
        criteria.add(Restrictions.not(Restrictions.in("category.id", excludedCategoryIds)));
    }
    if (groupId != null) {
        criteria.add(Restrictions.eq("groupId", groupId));
    }
    if (descending) {
        criteria.addOrder(Order.desc(orderByField));
    } else {
        criteria.addOrder(Order.asc(orderByField));
    }
    criteria.setFetchMode("category", FetchMode.JOIN);
    criteria.setFirstResult(offset).setMaxResults(pageSize);

    return criteria.list();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public List<Release> searchByCategoriesMaxAgeAndGroup(String[] searchTokens, Long imdbId, Long rageId,
        String season, String episode, Collection<Integer> categoryIds, Date maxAge,
        Collection<Integer> excludedCategoryIds, Long groupId, String orderByField, boolean descending,
        int offset, int pageSize) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);

    addSearchTokens(searchTokens, criteria);

    if (ValidatorUtil.isNotNull(imdbId)) {
        criteria.add(Restrictions.eq("imdbId", imdbId));
    }//from  w  ww  .  j ava2  s.co m

    if (ValidatorUtil.isNotNull(rageId)) {
        criteria.add(Restrictions.eq("rageId", rageId));
    }

    if (ValidatorUtil.isNotNull(season)) {
        criteria.add(Restrictions.eq("season", season));
    }

    if (ValidatorUtil.isNotNull(episode)) {
        criteria.add(Restrictions.eq("episode", episode));
    }

    if (!categoryIds.isEmpty()) {
        criteria.add(Restrictions.in("category.id", categoryIds));
    }
    if (maxAge != null) {
        criteria.add(Restrictions.gt("postDate", maxAge));
    }
    if (excludedCategoryIds != null && !excludedCategoryIds.isEmpty()) {
        criteria.add(Restrictions.not(Restrictions.in("category.id", excludedCategoryIds)));
    }
    if (groupId != null) {
        criteria.add(Restrictions.eq("groupId", groupId));
    }
    if (descending) {
        criteria.addOrder(Order.desc(orderByField));
    } else {
        criteria.addOrder(Order.asc(orderByField));
    }
    criteria.setFetchMode("category", FetchMode.JOIN);
    criteria.setFirstResult(offset).setMaxResults(pageSize);

    return criteria.list();
}