title: Complex query using Criteria API in Hibernate 4
body: <p>I am using hibernate 4 with criteria API and I am facing a complex query.</p>
  
  <p>My relational model is the following:</p>
  
  <p><a href="https://i.stack.imgur.com/drRzk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/drRzk.jpg" alt="relational model"></a></p>
  
  <p>What I am trying to do is to get for a given Person all the shipped <strong>Articles</strong> that belongs to the "n" last <code>ShoppingEvent</code> before a specified date.</p>
  
  <p>How can I achieve that using criteria API?</p>
  
  <p><em>n.b.</em> I already tried something like:</p>
  
  <pre><code>ProjectionList properties = Projections.projectionList();
  properties.add(Projections.property("article.articleId"), "articleId");
  properties.add(Projections.property("article.price"), "price");
  properties.add(Projections.property("article.type"), "type");
  
  return session.createCriteria(Person.class)//
           .add(Restrictions.idEq(person.getPersonId()))//
           .createAlias("articles", "article")//
           .createAlias("article.shoppingEvent", "se")//
           .add(Restrictions.le("se.date", currentDate))//
           .addOrder(Order.desc("se.date"))//
           .setProjection(properties)//
           .setResultTransformer(Transformers.aliasToBean(Articles.class))//
           .list();
  </code></pre>
  
  <p>Which returns the articles I want but I don't succeed to use the <code>setMaxResults</code> to limit the max number of <code>ShoppingEvents</code>.</p>