• 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
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!