spring - Getting LazyInitializationException despite being in @Transactional and having no detached entities -


i’m using spring 3.2.11.release, jpa 2.1, hibernate 4.3.6.final, , mysql 5.5.37. i’m getting below error in junit test

testsendvalidassignment(org.mainco.subco.thirdparty.service.thirdpartyserviceit)  time elapsed: 13.366 sec  <<< error! org.hibernate.lazyinitializationexception: failed lazily initialize collection of role: org.mainco.subco.lessonplan.domain.lessonplan.classrooms, not initialize proxy - no session     @ org.hibernate.collection.internal.abstractpersistentcollection.throwlazyinitializationexception(abstractpersistentcollection.java:575)     @ org.hibernate.collection.internal.abstractpersistentcollection.withtemporarysessionifneeded(abstractpersistentcollection.java:214)     @ org.hibernate.collection.internal.abstractpersistentcollection.readsize(abstractpersistentcollection.java:155)     @ org.hibernate.collection.internal.persistentbag.size(persistentbag.java:278)     @ org.mainco.subco.thirdparty.service.thirdpartyserviceit.testsendvalidassignment(thirdpartyserviceit.java:102) 

however, don’t know line blame error message. in method have

@service @transactional public class myserviceimpl implements myservice { ....  @override public void sendassignment(final string assignmentid) {     final assignment assignment = m_lessonplandao.getassignment(assignmentid);     if (processdata(assignment))     {         // gather students have been assigned assignment         final list<classroom> classes = lessonplan.getclassrooms();         system.out.println("got " + classes.size() + " classes.");         // send 1 request each class assignment         (final classroom classroom : classes)         {             final list<user> classstudents = m_classroomsvc.findclassstudents(classroom.getid());             system.out.println("got " + classstudents.size() + " students."); 

and both system.out lines print numbers. there no other references lazily-loaded collection in method don’t know else check in data. advice appreciated.

your service method transactional, entire test method not. size checking of element performed in assertion statement in junit test , done outside of transaction scope of service, causes lazy initialization exception. there 3 ways can go

  • you can try call size() method inside dao method make outside call of size() method safe.
  • you can force user of method open transaction(document objects detached , make contract/or use infamous open session in view pattern)
  • you can create dto layer , return size part of dto

all 3 solutions have pros , cons:

the first solution weird supporters of code when they'll find call getter no obvious reson - needs commenting.

the second makes life harder users of interface

the third violates dry principle

p.s. can disable lazy initialization of course


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -