Reputation: 607
i'm using hibernate and would like to get the entire hierarchy of Users->Posts->Comments but only the comments that have "status" to true.
i've tried something like this:
Select user from User user join user.posts post join post.comments comment where comment.status = true
But this give me an explosion of user for the number of comments with status true and inside the single user->post, i've all the comments, non only the ones with status true.
I've also tried with "left join" or "inner join" but i get the same result.
Is there a way to get what i would like? and wich is the "cleaner" way to do this?
Here is a little example of the class structure:
Class User
List<Post> posts
Class Post
List<Comment> comments
Class Comment
String text
boolean status
Thanks for any help!
Upvotes: 3
Views: 600
Reputation: 7739
I think what you want to do is filter the collection using the @FilterJoinTable annotation:
@FilterJoinTable(name="activeComments", condition="status = :status")
List<Comment> comments
You will need to declare the filter at the class level:
@FilterDef(name="activeComments", parameters={
@ParamDef( name="status", type="boolean" )
})
and enable it from the session:
Filter filter = session.enableFilter("activeComments");
filter.setParameter("status", true);
Here is a tutorial on Hibernate filtering, with examples: http://www.concretepage.com/hibernate/hibernate-filter-and-filterjointable-annotation-example
...and the official Hibernate 3 documentation: https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/filters.html
Upvotes: 1