Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to retreive a sorted list using hibernate

 
Mariano Javier
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i'm having trouble mapping two classes from a system.
I'm developening a weblog and my tables are something like this:

table blog(id, text)
table comment(id, text, postdate)
table blog_comments(id_blog,id_comment, order_id)

I have made already a mapping that goes as follows:

<class name="model.Blog" table="blog">
<id name="id">
<generator class="native"/>
</id>
<property name="text"/>
<list name="comments" table="blog_comments">
<key column="id_blog"/>
<list-index column="order_number"/>
<many-to-many class="model.Comment" column="id_comment"/>
</list>
</class>

<class name="model.Comment" table="comment">
<id name="id">
<generator class="native"/>
</id>
<property name="text"/>
<property name="postdate"/>
</class>

Actually, it works, but i'd like to have the list of comments ordered inverse, so i'll receive the last comment posted in the first place, is there a way to accomplish this without sorting it later in memory?
I've already tried to use a set with the sort-by property instead of a list and works, but if I add a comment after retreiving the set for the first time, this last comment would go to the end of the set.

I've searched the web and this forum also and i didn't find what I need because almost every example doesn't use a joing table to relate this two classes.

Thanks in advance, Mariano.
 
Alejandro Dominguez
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mariano,
the way I see it you have two choices:

1) using the sort-by property, you'd have to re-query (refresh) the entity, so the sort-by is applied

2) instead of a Set (interface) use a SortedSet and eliminate the sort-by property; now your Comment class must implement Comparable (overriding equals and hashCode is good too). This way, as soon as you add a Comment to your SortedSet, it will fall on the apropiate spot. This choice is my prefered. Your milleage may vary.

Hope it helps!

Alejandro
 
Mariano Javier
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Alejandro, i'll try your second recommendation, i have already no experience implementing Comparable and using it, any example or link will be really apreciated.

Thanks, Mariano
 
Alejandro Dominguez
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,
for the official definition, you can take a look at this:

http://java.sun.com/j2se/1.5/docs/api/java/lang/Comparable.html

As for the example, you'll find a lot by just googling it.
Comparable is even better when used with generics, see the attached example:



By using generic, the class already knows it will be compared to another Transaction object (NOTE: Transaction is a class of mine, it's NOT Hibernate Transaction).

Hope it helps

Regards

Alejandro
 
Mariano Javier
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your quick answer
[ June 08, 2007: Message edited by: Mariano Javier ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic