0

I have a class Video that has a OneToMany annotation to another class Comment.

@OneToMany(fetch = FetchType.LAZY, mappedBy = "videoId", cascade = CascadeType.ALL, orphanRemoval = true)
@Sort(type = SortType.COMPARATOR, comparator = CommentComparator.class)

I'm setting setMaxResults(4) in my query hoping to retrieve only the last 4 videos. This works great if there is no comments exist for any of the videos. However, if I specify more than 1 comment per video, my query returns less than 4 videos now (I think it counts the fetched rows as towards maxResult).

How do I make sure that all 4 videos are returned in my query?

public <T>List<T> find(Class<T> clazz, Order order, int maxResults, List<String> fetch, List<Criterion> expressions)
{
    Criteria criteria = getCurrentSession().createCriteria(clazz);
    for (Criterion ex : expressions)
    {
        criteria.add(ex);
    }
    if (maxResults > 0)
    {
        criteria.setMaxResults(maxResults);
    }

    if (fetch != null)
    {
        for (String f : fetch)
        {
            criteria.setFetchMode(f, FetchMode.JOIN);
        }
    }
    criteria.addOrder(order);

    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return criteria.list();
}

I'm calling

return myDao.find(Video.class, Order.desc("id"), maxResult, fetch, criteria);
tumanov
  • 539
  • 1
  • 7
  • 14

0 Answers0