Hi everybody,
I got this many-to-many bidirectional association:
EVENT <-> EVENT_CATEGORY <-> CATEGORY
So basically I would like to get some events from the category x, and get the categories of the event y.
I do it like this in Category.hbm.xml :
Code:
<!-- Bi-directionnal association -->
<set name="events"
table="EVENT_CATEGORY"
order-by="event.CREATION_DATE desc"
inverse="true"
lazy="true" >
<key column="CATEGORY_NAME"/>
<many-to-many column="ID_EVENT" class="scem.domain.model.Event"/>
</set>
As a lot of people I would like to display events contained in a category by using a pagination system.
I know how to do it with firstResult and maxResult in the case I want to display all events, whatever is the category.
Actually, with this many-to-many association, I fetch all events of the category, get them sorted in a set, convert them to a list (it looks like it's not possible to use directly a list with the "order-by" parameter...), and do:
Code:
listEvents.subList(firstIndex, lastIndex)
It means all events of a category are sorted, fetched, and then I use just some of them. Isn't it wrong ? A lot of data are fetched and not used...
Moreover, I have to double check if the firstIndex and lastIndex are not out the available values.
It would be better to use a pagination by fetching only usefull nodes.
But how can I do this in the case of a many-to-many association ?
Of course, I can write my own SQL query in CategoryDAO, defining the junctions between EVEN, EVENT_CATEGORY, CATEGORY, specify the category I need and combine it with firstResult, maxResult...
But, tell me if I'm wrong, isn't it the aim of Hibernate to simplify the job, and offer me a simple category.getEvents() method instead of implementing my own categoryDAO.fetchPage(page_number, result_size) as I said above...
Any idea so far ?
What's the best :
- fetch all results and then use just some of them
- write a method in the DAO, using firstResult, maxResult and do by myself the junctions
- or there is a better solution I'm not aware of ?
Thanks for any help, advice.