• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Criteria

 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have a nested object for which i want to write a criteria to retrieve the restricted set of objects.

Example:

User has --> collection of companies of type (Company)--> Company has collection of Contracts of type (Contract) --> Contract has collection of Bills of type (Bill). Bill has an date based on which i want to retrieve all the bills of the user.

My criteria is as follows:

Criteria userCriteria = this.getSession().createCriteria(User.class)
.add(Expression.eq("userId", userVO.getUserId()));

Criteria contractCriteria = userCriteria.createCriteria("companies")
.createCriteria("contracts");

Criteria billsCriteria = contractCriteria.createCriteria("bills");
billsCriteria.addOrder(Order.desc("issueDate")).setMaxResults(1);

Collection users = billsCriteria.list();

This gives me collection of users ahving 1 user object but it gives all the bills of the user. I want to get the latest 5 bills based on issue date. How this can be one by tweaking this criteria ?

Any help will be appreciated.

Thanks in advance,
Saurabh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic