title: Spring Junit Hibernate @Transactional - no Session
body: <p>I find a lot of people get this exception, but cannot find 
  why this happen with me :). How could it be 
  within <code>@Transactional</code> method, when 
  I refer to object loaded from db within same method?</p>
  
  <p>The entity should not be detached, and session should be same ... but this not works.</p>
  
  <p>I have a lazily initiated property in my object, and when try to 
  read from it, Hibernate states "no session", but what lead to close it? As I understand hibernate session should be alive inside <code>@Transactional</code> method until it done.</p>
  
  <p>I tried to mark <code>@Transactional</code> my <code>@Test</code> method and it works.</p>
  
  <p>But I would like to have few <code>@Transactional</code> methods calls within one test method,because I have to save entities and load them in separate sessions. This is because I use <code>@OrderBy</code> and it works only if object loaded from DataBase.</p>
  
  <p>Here test class: </p>
  
  <pre><code>@Test
  public void getOpenTest() {
      ...
      Advertisement closedAdvertisement = closeAdvertisement(initialAdvertisement.getId());
      ...
  }
  
  //this method inside same test class
  @Transactional
  private Advertisement closeAdvertisement(Long id){
      Advertisement advertisement = advertisementRepository.findOne(id);
  
      //*** CRASHES HERE ***
      //The method is @Transactional. Why session closed here?
      advertisement.getStatusChronology().get(0);
  
      ...
  }
  </code></pre>
  
  <p>Repository</p>
  
  <pre><code>@Repository
  public interface AdvertisementRepository  extends CrudRepository&lt;Advertisement, Long&gt; {
      //Simple request just to load entity 
      @Query("select ad  "
              + " from Advertisement ad")
      public List&lt;Advertisement&gt;getOpen();
  }
  </code></pre>
  
  <p>Entities</p>
  
  <pre><code>@Entity
  @Table(name = "advertisement")
  public class Advertisement {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO) 
      Long id;
      ...
      @OneToMany(mappedBy = "advertisement", cascade = CascadeType.ALL)
      @OrderBy("updated DESC")
      List&lt;Status&gt; statusChronology;
      ...
  }
  
  @Entity
  @Table(name = "status")
  public class Status {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO) 
      Long id;
  
      @ManyToOne
      @JoinColumn(name = "statusChronology")  
      private Advertisement advertisement;
  
      @NotNull
      @Column(name = "is_open")
      private Boolean isOpen;
  
      @NotNull
      @Column
      private LocalDateTime updated;  
  
      ...
  }
  </code></pre>
  
  <p>Exception</p>
  
  <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: web.scraper.database.model.Advertisement.statusChronology, could not initialize proxy - no Session
      ...
      at web.scraper.database.repository.AdvertisementRepositoryTest.closeAdvertisement(AdvertisementRepositoryTest.java:80)
      at web.scraper.database.repository.AdvertisementRepositoryTest.getOpenTest(AdvertisementRepositoryTest.java:107)
      ...
  </code></pre>