title: Hibernate failed to lazily initialize a collection of role no session in entity service even with @Transactional
body: <p>I have a hibernate project that connects to two data sources and has a base dao. I use the base dao to derive generic daos for the data sources as well.</p>
  
  <p>I have a service that fetches a join relationship and I am running into the following error when ever an entity has a one to many relationship. I am stuck in that I am not able to get the one to many relationship objects.</p>
  
  <p>I used the apache commons beanutils to copy properties hoping it would do a deep copy but it does not work either. I am running out of ideas.</p>
  
  <p>I read that I there is the option to use Open Session In View but I am not sure how to go about that.</p>
  
  <p>Error</p>
  
  <pre><code>failed to lazily initialize a collection of role: com.sony.spe.mc.domain.OperationalUnit.user, could not initialize proxy - no Session
  </code></pre>
  
  <p>I have the following layers and files</p>
  
  <p>Controller Layer</p>
  
  <pre><code>    @ApiOperation(value = "read", nickname = "getByID")
      @RequestMapping(value="/read", method = RequestMethod.GET)
      @ApiImplicitParams({
          @ApiImplicitParam(name = "id", value = "User's id", required = true, dataType = "int", paramType = "query")
      })
      @ApiResponses(value = { 
              @ApiResponse(code = 200, message = "Success", response = User.class),
              @ApiResponse(code = 500, message = "Failure")})
      @ResponseBody
      public UserProxy getByID(long id) {
          UserProxy user;
          try {               
              user = userService.fetchUserById(id);
          }
          catch(Exception ex) {
              ex.printStackTrace();
              return null;
          }
          return user;
      }
  </code></pre>
  
  <p>The Service Layer</p>
  
  <p>User Service</p>
  
  <pre><code>@Service
  @Transactional("fuseTransactionManager")
  public class UserServiceImpl extends BaseFuseServiceImpl&lt;User&gt; implements UserService {
      @Autowired
      UserDao userDao;
  
      @Override 
      protected UserDao getDao() {
          return userDao;
      }
  
      @Override
      public UserProxy fetchUserById(long id) {
          try {
  /***** ERROR HAPPENS HERE. THE USER OBJECT IS NOT ABLE TO GET THE ONE TO MANY OBJECTS even though I have @Transactional *****/
              User user = userDao.fetchEntityById(User.class, id);
              UserProxy userProxy = new UserProxy();
              BeanUtils.copyProperties(userProxy, user);
              return userProxy;
          } catch(Exception ex) {
              ex.printStackTrace();
              return null;
          }
      }
  .....
  </code></pre>
  
  <p>Generic Fuse Service</p>
  
  <pre><code>@Service
  public class BaseFuseServiceImpl&lt;T extends Serializable&gt; extends BaseServiceImpl&lt;T&gt; implements BaseFuseService&lt;T&gt; {
      @Autowired
      BaseFuseDao&lt;T&gt; baseFuseDao;
  
      @Override
      protected BaseFuseDao&lt;T&gt; getDao() {
          return baseFuseDao;
      }
  }
  </code></pre>
  
  <p>Very generic service that the specific services implement</p>
  
  <pre><code>@Service
  @Transactional("baseTransactionManager")
  public abstract class BaseServiceImpl&lt;T extends Serializable&gt; implements BaseService&lt;T&gt; {
      protected abstract BaseDao&lt;T&gt; getDao();
  
      @Override
      public T fetchEntityById(Class&lt;T&gt; entityClass, long id) {
          return getDao().fetchEntityById(entityClass, id);
      }
  </code></pre>
  
  <p>User Entity. There is also a corresponding proxy object that is pretty much identical except it does not have the @Entity @Column annotations.</p>
  
  <pre><code>@Entity
  @Table(name="USER")
  //@AttributeOverride(name = "ID", column = @Column(name = "USER_ID",  nullable = false))
  public class User extends BaseEntity {
  //public class User implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = 5236507646361562577L;
  
      @Id  
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "USER_ID")
      private long id;  
  
      @OneToMany(cascade=CascadeType.ALL)  
      @JoinTable(name="USER_OPERATIONALUNIT",  
          joinColumns = {@JoinColumn(name="USER_ID")},  
          inverseJoinColumns = {@JoinColumn(name="OPERATIONALUNIT_ID")}
      )  
      private Set&lt;OperationalUnit&gt; operationalUnit;
  .... getters and setters
  </code></pre>