Which is the best method to use to only load a limited amount
of items from an assocated collection?
The idea is to only load a certain ammount of objects.
I have one User object with 4 collections associated with it.
But I only want to display 20 items from each collection.
Method 1 : Use Lazy-loading of the collections.
Code:
User user = userService.findUserById(userId);
Product product = user.getProducts();
Comment comment = user.getComments();
Friends friends = user.getFriends();
Full Class+DAO code is here,
http://forum.springsource.org/showthread.php?t=75099The Collections are loaded in the same Hibernate session
but the whole collection is loaded.
Method 2 :
http://forum.springsource.org/showthread.php?t=65836Hit the database each time to load a certain collection.
and set the maximum amount of results to be returned in each of the DaoImpl.
Code:
User user = userService.findUserById(userId);
Product product = productService.findProductsById(userId);
Comment comment = commentService.findCommentsById(userId);
Friends friends = friendsService.findFriendsById(userId);
session.createQuery(xxxxx).setFirstResult(0).setMaxResults(15).list();
Gets the required amount, but a new Hibernate session
and database hit is required each time.