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:
the serviceFacade's method looks as follows: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 classA's method addB looks as follows:Code:objectA.addB(objectB); aDAO.save(objectA);
The lazy initializaton exception is thrown inCode:this.listOfBs.add(objectB); objectB.getAs.add(this);what seems to make sense, as the list is marked to be lazily fetched in the Hibernate mapping file.Code:this.listOfBs.add(objectB);
My question is where and how to reattach the objects to a new session without affecting business code.
I've tried to introduce a methodto my DAOs and call them in assignBToA before the business method call (objectA.addB(objectB), but I still get the lazy init exception.Code:reattach(BO oBO) { getHibernateTemplate.saveOrUpdate(oBO); }
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


Reply With Quote