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:
List<A> list = serviceFacade.getAll() //returns all Entries frm the DB
Iterator<A> it = list.iterator();
A objectA = it.next();
B objectB = newB();
the classA's method addB looks as follows:
The lazy initializaton exception is thrown in
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
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.