LazyInitializationException 问题和正确设置 ManyToOne

Problem with LazyInitializationException and Correct setting ManyToOne

我有 3 个像这样相互连接的对象:

@Entity
public class Room {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column
    private String name;

    @OneToMany(mappedBy = "room", fetch = FetchType.EAGER)
    private List<SponsorStart> sponsorsStart = new ArrayList<>();

    @OneToMany(mappedBy = "room", fetch = FetchType.LAZY)
    private List<SponsorFinish> sponsorsFinish = new ArrayList<>();
    //getters and setters
    ```
@Entity
public class SponsorStart {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "room_id")
    private Room room;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Post> posts;
//getters and setters
@Entity
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column
    private long pk;
    @Column
    private String postId;
    //getters and setters

如果设置类似 top 的设置,则结果如下: 请注意显示了 4 行(如果从 0 开始则为 3 行) 但是,如果我想从 SponsorStart 对象中获取 post 列表,我会得到:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.spring.givestracker.model.SponsorStart.posts, could not initialize proxy - no Session
        at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:597)
        at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:216)
        at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:576)
        at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:147)
        at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:303)
        at com.spring.givestracker.service.impl.SponsorsFinishServiceImpl.parseSponsorFinishByRoom(SponsorsFinishServiceImpl.java:78)
Here is lazy:
    for (SponsorStart sponsorStart : room.getSponsorsStart()) {
            Hibernate.initialize(sponsorStart.getPosts());
         }

好的,我以为我知道问题并在 SponsorStart 实体中添加:

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Post> posts;

但是!我有这个问题! 是的,这是问题!我在 4 个 SonsorStart 实体中总共有 43 个 post,为什么它会这样显示,我应该如何修复它?

帮助)

更新 1

@Entity
public class Room {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column
    private String name;

    @OneToMany(mappedBy = "room", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<SponsorStart> sponsorsStart = new ArrayList<>();

    @OneToMany(mappedBy = "room", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<SponsorFinish> sponsorsFinish = new ArrayList<>();
    //getters and setters

@Entity
public class SponsorStart {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne (fetch=FetchType.LAZY,
            cascade=CascadeType.ALL)
    @JoinColumn(name = "room_id")
    private Room room;

     @OneToMany(mappedBy = "sponsorStart", cascade = CascadeType.ALL)
    private List<Post> posts;
//getters and setters
@Entity
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column
    private long pk;
    @Column
    private String postId;
    @ManyToOne(fetch=FetchType.LAZY)
    private SponsorStart sponsorStart;

 //getters and setters

而且我面临延迟初始化

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.spring.givestracker.model.SponsorStart.posts, could not initialize proxy - no Session
        at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:597)
        at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:216)
        at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:576)
        at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:744)
        at org.hibernate.Hibernate.initialize(Hibernate.java:64)
        at com.spring.givestracker.service.impl.SponsorsFinishServiceImpl.parseSponsorFinishByRoom(SponsorsFinishServiceImpl.java:73)

从使用 RoomService 的 RoomController 中,我得到 findRoomById(id),RoomService 具有@Transactional,从 RoomController 中,我将 Room was found 给另一个服务。当我委托另一个服务使用 RoomService 来查找 RoomById(id) 时,Lazy 的问题消失了!但是我不知道当我有 4 个赞助商但休眠加载我 43 次 4 个赞助商时那是什么)因为赞助商获得 43 个帖子...