微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

LazyInitializationException 问题和正确设置 ManyToOne

如何解决LazyInitializationException 问题和正确设置 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 这样的设置,那么结果如下:

enter image description here

请注意,显示了 4 行(如果从 0 开始,则为 3 行) 但是,如果我想获取帖子表单 SponsorStart 对象的列表,我会得到:

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;

但是!我有这个问题!

enter image description here

是的,这里有问题!我在 4 个 SonsorStart 实体中总共有 43 个帖子,为什么它会这样显示,我应该怎么做才能修复它?

帮助)

更新 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)

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。