2 Replies Latest reply on Jan 28, 2006 4:48 AM by Cyril JOUI

    Lazy Fetching Problem

    Stefan Kanev Newbie

      Hello everybody.

      I'm experiencing a problem with the Lazy fetching. Say, I'm developing a dumb organizer web application, that has Users and Tasks (One-To-Many). I've set FetchType to Lazy in the relationship, because I don't want to fetch all tasks every time. However, I need to display the tasks a user has in the application front-end, i.e., in a web page. For this, I have a session beans that has a method

      List<Task> getUserTasks(int userId)

      However, the implementation:
      User user = em.find(User.class, userId);
      return user.getTasks()

        • 1. Re: Lazy Fetching Problem
          Stefan Kanev Newbie

          However, the above implementation throws an exception when I'm trying to retrieve the Task properties outside the session bean. I know the reason is, that once the detached Task bean is outside the persistence context, it can't be loaded from the database, and since it is was not used in the session bean, it does not get filled at all. However, I need to be able to display all tasks a user has. How am I supposed to do that, without setting the FetchType to Eager, and possibly without using DTOs to transfer objects in and out of the session beans.

          P.S.: Sorry for the two posts, I hitted submit instead of preview and was not able to edit my post.

          • 2. Re: Lazy Fetching Problem
            Cyril JOUI Newbie


            You have just have to call property.size() (of your collection) in your session bean before returning it to other layer.

            Good luck