aspose file tools*
The moose likes Object Relational Mapping and the fly likes Using Hibernate Criteria Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Using Hibernate Criteria" Watch "Using Hibernate Criteria" New topic
Author

Using Hibernate Criteria

Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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,

SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Mansi Mishra
Ranch Hand

Joined: Dec 26, 2008
Posts: 50
Try this...



http://splashpress.blogspot.com
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
Mansi Mishra. wrote:Try this...



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

Thanks,
Mansi Mishra
Ranch Hand

Joined: Dec 26, 2008
Posts: 50
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

Joined: Aug 20, 2003
Posts: 532
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

Joined: Aug 17, 2009
Posts: 75
http://docs.jboss.org/hibernate/stable/core/reference/en/html/querycriteria.html
Mansi Mishra
Ranch Hand

Joined: Dec 26, 2008
Posts: 50
Oh okay...
Then may be you could try using projections. That should let you fetch a list of books.
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Hibernate Criteria