简体   繁体   English

Spring Junit Hibernate @Transactional - 没有会话

[英]Spring Junit Hibernate @Transactional - no Session

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? 当我在同一个方法中引用从db加载的对象时,它怎么可能在@Transactional方法中呢?

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? 我在我的对象中有一个懒惰的启动属性,当试图从它读取时,Hibernate声明“没有会话”,但是什么导致关闭它? As I understand hibernate session should be alive inside @Transactional method until it done. 据我所知,hibernate会话应该在@Transactional方法中存活,直到它完成。

I tried to mark @Transactional my @Test method and it works. 我试着将@Transactional标记为@Test方法并且它有效。

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. 但我想在一个测试方法中调用几个@Transactional方法,因为我必须保存实体并在单独的会话中加载它们。 This is because I use @OrderBy and it works only if object loaded from DataBase. 这是因为我使用@OrderBy ,它仅在从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)
    ...

@Transactional不适用于私有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM