title: Many-to-Many - lazily initialization failing on a collection
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5095559/spring-hibernate-many-to-many-lazyinitializationexception">Spring, Hibernate - many-to-many - LazyInitializationException</a>  </p>
  </blockquote>
  
  
  
  <p>I have facing quite a strange issue, which I have failed to understand.
  There are two entities "User" and "Contact".
  The exception gets thrown in the ProjectController.java at <code>user.getContactSet().add(contact);</code></p>
  
  <p>I am trying to get Many-to-Many relation between "User" and "Contact"</p>
  
  <p>User.java</p>
  
  <pre><code>   public class User {
      private Integer userID;
      private String userLoginEmail;
      private String password;
      private String userFirstName;
      .
      .
  </code></pre>
  
  <p>Contact.java</p>
  
  <pre><code>   public class Contact {
     private Integer contactID;
     private String givenName;
     private String familyName;
     private String telephoneNumber;
     private String cellNumber;
     .
     .
  </code></pre>
  
  <p>User.hbm.xml</p>
  
  <pre><code>&lt;hibernate-mapping package="com.smallworks.model" schema="smallworksdb"&gt;
  
  &lt;class name="User" table="USERACCOUNT"&gt;
      &lt;id column="USER_ID" length="500" name="userID"&gt;
          &lt;generator class="increment" /&gt;
      &lt;/id&gt;
      &lt;property column="USER_LOGIN_EMAIL" generated="never" lazy="false" length="100" name="userLoginEmail" /&gt;
      &lt;property column="USER_PASSWORD" generated="never" lazy="false" length="50" name="password" /&gt;
      &lt;property column="ACTIVATION_CODE" generated="never" lazy="false" length="100" name="activationCode" /&gt;
      &lt;property column="ACCOUNT_ACTIVATED" generated="never" lazy="false" length="1" name="accountActivated" /&gt;
  
      &lt;set name="contactSet" table="USER_CONTACT" 
          inverse="false" lazy="true" fetch="select" cascade="all"&gt;
          &lt;key column="USER_ID"/&gt;
          &lt;many-to-many column="CONTACT_ID" class="Contact"/&gt;
      &lt;/set&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p>Contact.hbm.xml</p>
  
  <pre><code>&lt;hibernate-mapping package="com.smallworks.model" schema="smallworksdb"&gt;
  &lt;class name="Contact" table="CONTACT"&gt;
      &lt;id column="CONTACT_ID" length="500" name="contactID"&gt;
          &lt;generator class="increment" /&gt;
      &lt;/id&gt;
      &lt;property column="GIVEN_NAME" generated="never" lazy="false" length="100" name="givenName" /&gt;
      &lt;property column="FAMILY_NAME" generated="never" lazy="false" length="100" name="familyName" /&gt;
      &lt;property column="TELEPHONE_NUMBER" generated="never" lazy="false" length="100" name="telephoneNumber" /&gt;
      &lt;property column="CELL_NUMBER" generated="never" lazy="false" length="100" name="cellNumber" /&gt;
      &lt;property column="EMAIL" generated="never" lazy="false" length="100" name="email" /&gt;
      &lt;property column="STREET_NAME" generated="never" lazy="false" length="100" name="streetName" /&gt;
      &lt;property column="CITY" generated="never" lazy="false" length="100" name="city" /&gt;
      &lt;property column="STREET_ADDRESS" generated="never" lazy="false" length="100" name="streetAddress" /&gt;
      &lt;property column="ADDRESS_LOCALITY" generated="never" lazy="false" length="100" name="addressLocality" /&gt;
      &lt;property column="ADDRESS_REGION" generated="never" lazy="false" length="100" name="addressRegion" /&gt;
      &lt;property column="POSTAL_CODE" generated="never" lazy="false" length="100" name="postalCode" /&gt;
      &lt;property column="COMMENT" generated="never" lazy="false" length="100" name="comment" /&gt;
  
      &lt;!-- many to many mapping with the User via User_Contact table --&gt;
      &lt;set name="userSet" table="USER_CONTACT" 
          inverse="true" lazy="true" fetch="select"&gt;
          &lt;key column="USER_ID" /&gt;
          &lt;many-to-many column="CONTACT_ID" class="Contact" /&gt;
      &lt;/set&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p>ProjectController.java</p>
  
  <pre><code>@RequestMapping(value = { "/project/addContact.do" }, method = RequestMethod.POST)
  @ResponseBody
  @JsonIgnore
  public  String addContact(@ModelAttribute("UserContactAction") ContactAction contactAction, BindingResult result, HttpServletRequest request, Model model) {
      User user=(User) request.getSession().getAttribute("user");
      if(contactAction!=null){
          if(contactAction.getContact()!=null){
              Contact contact=contactAction.getContact();
              if(contact.getUserSet()==null){
                  contact.setUserSet(new HashSet&lt;User&gt;());
              }
              user.getContactSet().add(contact);  //ERROR Gets thrown here
          }
  
      }else{
          System.out.println("userContactAction is NULL");
      }
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>    01:13:26.953 [http-bio-8080-exec-9] DEBUG o.s.w.b.a.s.HandlerMethodInvoker - Invoking request handler method: public java.lang.String com.smallworks.controller.ProjectController.addContact(com.smallworks.model.view.ContactAction,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest,org.springframework.ui.Model)
  01:13:26.961 [http-bio-8080-exec-9] ERROR o.h.LazyInitializationException - failed to lazily initialize a collection of role: com.smallworks.model.User.contactSet, no session or session was closed
  org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.smallworks.model.User.contactSet, no session or session was closed
      at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
      at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
      at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
      at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
      at com.smallworks.controller.ProjectController.addContact(ProjectController.java:96) [ProjectController.class:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.6.0_33]
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [na:1.6.0_33]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [na:1.6.0_33]
      at java.lang.reflect.Method.invoke(Unknown Source) [na:1.6.0_33]
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) [servlet-api.jar:na]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.33]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.33]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.33]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.33]
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.33]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.33]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.33]
  </code></pre>
  
  <p>I am confused, as to why this exception is coming, any sort of help will be appreciated.
  I also tried to take User and Contact away and instead tried to do many to many between Employee and Meeting (just created some independent Objects), then it all works fine.
  Please help...</p>