Announcement Announcement Module
No announcement yet.
Lazy-Load 4 whole associated Collections VS 4 DB hits to limit amount Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy-Load 4 whole associated Collections VS 4 DB hits to limit amount

    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.

    User user = userService.findUserById(userId);
    Product product = user.getProducts();
    Comment comment = user.getComments();
    Friends friends = user.getFriends();
    Full Class+DAO code is here,

    The Collections are loaded in the same Hibernate session
    but the whole collection is loaded.

    Method 2

    Hit the database each time to load a certain collection.
    and set the maximum amount of results to be returned in each of the DaoImpl.

    User user = userService.findUserById(userId);
    Product product = productService.findProductsById(userId);
    Comment comment = commentService.findCommentsById(userId);
    Friends friends = friendsService.findFriendsById(userId);

    Gets the required amount, but a new Hibernate session
    and database hit is required each time.

  • #2
    The second method doesn't HAVE to use a new Hibernate Session for each query but either solution is going to require more than one database hit, I don't think you can load more that one collection with the initial entity.


    • #3
      Hey karldmoore,

      Using method 1, I am able to load 7 collections along with the User object in one batch load, and count the collection.

      But now the collections are getting large and need to be optimized somehow.
      Are there no constraints that can be put on Lazy Collections? I've been searching and reading, can't seem to find anything. Meaning its impossible.

      Using method 2, I currently have each service using its own Dao to access the database.

      Are you saying that I can place 4 "session.createQueries(xxxx)" in one Dao? then build up my user model with these results and return it.