Announcement Announcement Module
Collapse
No announcement yet.
Once again: LazyInitialization Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Once again: LazyInitialization

    Hi folks,

    I'm trying to do the following and don't know how to avoid a lazy initialization exception:

    I have 2 POJOs that are in a bidirectional n:m relation and store each others references in a list. I have implemented DAOs for both of them and want to access the via a service facade. Now I've written a JUnit test to test my service. One method covers the following use case:

    Assign all A's to B's. I tried to do something like this:
    Code:
    List<A> list = serviceFacade.getAll() //returns all Entries frm the DB
    Iterator<A> it = list.iterator();
    while (it.hasNext())
    {
    	A objectA = it.next();
    						
    	B objectB = newB();
    	objectB.setName("B "+objectA.getId());
    			
    	serviceFacade.assignBToA(objectB, objectA);
    }
    the serviceFacade's method looks as follows:

    Code:
    objectA.addB(objectB);
    aDAO.save(objectA);
    the classA's method addB looks as follows:
    Code:
    this.listOfBs.add(objectB);
    objectB.getAs.add(this);
    The lazy initializaton exception is thrown in
    Code:
    this.listOfBs.add(objectB);
    what seems to make sense, as the list is marked to be lazily fetched in the Hibernate mapping file.

    My question is where and how to reattach the objects to a new session without affecting business code.

    I've tried to introduce a method
    Code:
    reattach(BO oBO)
    {
    	getHibernateTemplate.saveOrUpdate(oBO);
    }
    to my DAOs and call them in assignBToA before the business method call (objectA.addB(objectB), but I still get the lazy init exception.

    Any tutorials i can find, talk about the OpenSessionInViewFilter but i don't see a way to use it in a JUnit test.

    Regards,

    Ollie

  • #2
    Hi Oliver!
    I have also this problem. The reason is that every collection in hibernate has a session. in a detached object the session ist closed. when you will access to the collection the object in the collection can not be fetched because no session is opened for this collection.
    this is only the reson but I do not know a answer how it could works.
    I have tried to reinitialize de collection with
    Code:
    Hibernate.initialize(obj)
    Hibernate.initialize(obj.getCollection())
    but I get the HibernateException with the message disconnected Session

    so maybe anyone can help us, because Lazy-Loading is e very complicating theme...

    thanx!!
    tom

    Comment


    • #3
      Hello Oliver!
      in the Hibernate Forum I get the following help (thanx to sw79)

      Code:
      public void addAToB(A a, B b) {
          Session s = this.getSession();
          B newb = (B)s.get(b.getClass(), b.getId());
          newb.getAs().add(a);
          this.saveOrUpdate(b);
        }
      I hope you can fix your problem

      Maybe you get the session in this way:
      Code:
       Session s = this.getHibernateTemplate().getSessionFactory().openSession();
      than you hav a new session opened, but in a DAO object this is not necessary.

      Good Luck!

      tom
      Last edited by tomSIEM; Apr 12th, 2006, 06:28 AM.

      Comment


      • #4
        I recommend to take a look at
        org.springframework.test.AbstractTransactionalData SourceSpringContextTests
        which is made exactly for your case. It will open a transaction for each test
        and roll it back at the end. This is not just very useful when doing
        integration testing (like you are) but also keeps a session opened for the duration
        of your tests. It also contains endTransaction() and startNewTransaction()
        methods which allows you test lazy inits or how your services behave
        after crossing different transaction bounderies.
        The test has some nice documentation inside the reference and you
        take a look at the junit tests inside Spring for more info. It has also
        been discussed several times on the forum.

        Comment

        Working...
        X