Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Hibernate Criteria

 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a class like this:


How can I create a criteria on this class that return a list of books only filtered by category?
P.S. I simplified the example I have to work with.

Thanks,
 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this...

 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansi Mishra. wrote:Try this...



Can you elaborate on how this code going to return a list of books?

Thanks,
 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hanna Habashy wrote:
Mansi Mishra. wrote:Try this...

Can you elaborate on how this code going to return a list of books?

Thanks,


getHibernateTemplate.findByCriteria(criteria).add(Restrictions.eq("category", filterValue))

returns a list of User2Book objects filtered by "category" using the "filterValue"
All you need to do is fetch this into an arraylist of the same type



returns a list of User2Book objects.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansi Mishra. wrote:
Hanna Habashy wrote:
Mansi Mishra. wrote:Try this...

Can you elaborate on how this code going to return a list of books?

Thanks,


getHibernateTemplate.findByCriteria(criteria).add(Restrictions.eq("category", filterValue))

returns a list of User2Book objects filtered by "category" using the "filterValue"
All you need to do is fetch this into an arraylist of the same type



returns a list of User2Book objects.


Thanks for the response, but my question is how to return a list of books, not a list of User2Book.
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh okay...
Then may be you could try using projections. That should let you fetch a list of books.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I get it solved.

criteria = createCriteria(User2Book.class, "u2b")
crit.add(Restrictions.eq("u2p.category", value));
crit.setProjection(Property.forName("book"));
crit = crit.createCriteria("user");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic