• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Hibernate Criteria

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this...

 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://docs.jboss.org/hibernate/stable/core/reference/en/html/querycriteria.html
 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
reply
    Bookmark Topic Watch Topic
  • New Topic