Reputation: 2942
I find a lot of people get this exception, but cannot find
why this happen with me :). How could it be
within @Transactional
method, when
I refer to object loaded from db within same method?
The entity should not be detached, and session should be same ... but this not works.
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 @Transactional
method until it done.
I tried to mark @Transactional
my @Test
method and it works.
But I would like to have few @Transactional
methods calls within one test method,because I have to save entities and load them in separate sessions. This is because I use @OrderBy
and it works only if object loaded from DataBase.
Here test class:
@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);
...
}
Repository
@Repository
public interface AdvertisementRepository extends CrudRepository<Advertisement, Long> {
//Simple request just to load entity
@Query("select ad "
+ " from Advertisement ad")
public List<Advertisement>getOpen();
}
Entities
@Entity
@Table(name = "advertisement")
public class Advertisement {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
...
@OneToMany(mappedBy = "advertisement", cascade = CascadeType.ALL)
@OrderBy("updated DESC")
List<Status> 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;
...
}
Exception
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)
...
Upvotes: 4
Views: 706