Announcement Announcement Module
Collapse
No announcement yet.
Lazy Initilization Exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy Initilization Exception

    Hi All,

    I am getting a lazyinitializationexception when I try to access collection in my service layer. I added opensessionInViewFilter to web.xml and still I am getting the following exception

    Code:
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: xxx.xx.xxxx.xxxxx.domain.Entity.entityTypes, no session or session was closed
    Please help me in resolving this issue.

    Thanks,
    Gupta

  • #2
    When you have classes that have information that spans tables like this Hibernate will only pull from the main table, ignoring any associations that're mapped farther down. What Hibernate does is it puts in a ghost-like object for those other properties instead so that when you try and reference it, it'll go to the database and get it. If you always need to have this information available, go to the association and set lazy="false", otherwise, in your DAO, open a session, grab your main entity, decide if you need the collection within it to be populated or not, close the session, and return the entity.

    It would make sense to me to be able to grab that information at a later point in time, but for whatever reason, this is how Hibernate works.

    Alternatively, access your collection like you do your entity and manually put it in there once you're done.

    Comment


    • #3
      I understand your explanation. How ever, I don't want to fetch all the associations while building the main object (by lazy=false). This will fire unnecessary sql statements behind the scene.

      When I refer old posts in the forums, they have resolved this issue by using OpenSessionInviewFilter in web.xml and somehow, I am getting the exception even after using OpenSessionInviewFilter. And I tried using
      Code:
      Hibernate.initialize(myObject);
      Hibernate.initialize(myObject.getEntityTypes());
      and in this case, I am getting a disconnected session exception.

      I ran out of ideas and don't know how to resolve this problem.

      Thanks,
      Gupta

      Comment


      • #4
        You can specify at the time you call the function if you want to use lazy fetching or not.

        Code:
        	public Rating getRating(int ratingId) {
        		Rating rating = new Rating();
        		rating.setId(ratingId);
        		DetachedCriteria criteria = DetachedCriteria.forClass(Rating.class);
        		criteria.setFetchMode("<name of association>, FetchMode.LAZY)
        		criteria.add(Restrictions.eq("id", ratingId));
        		List<Rating> results = getHibernateTemplate().findByCriteria(criteria);
        		if (results.size() == 1) {
        			rating = results.get(0);
        		} else {
        			rating = null;
        		}
        		return rating;
        	}
        Then create another method, call it getRatingsEager() and set that parameter the other way and it should do what you want.

        Comment


        • #5
          I have resolved this problem by changing my session declaration
          Code:
          Session session = SessionFactoryUtils.getSession(getHibernateTemplate().getSessionFactory(), true);
          earlier I was using
          Code:
          Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
          Thank you all
          Gupta

          Comment

          Working...
          X