无法延迟初始化角色集合

无法延迟初始化角色集合

我有一个名为category的表,该表具有父类别字段。我正在使用该字段来获取子类别。我已经检查了类似问题的答案,建议是(fetch = FetchType.EAGER)。但我想将其加载为LAZY,因为它是循环依赖项。 (再次指向同一张表)。

@Entity
@Table(name = "CATEGORY")
public class Category implements Serializable {
    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Integer id;

    @Column(name = "CATEGORY_NAME", nullable = false, length = 40)
    private String name;

    @Column(name = "DESCRIPTION", nullable = false, length = 255)
    private String description;

    @Column(name = "DATE_CREATED", nullable = false)
    private Date dateCreated;

    @Column(name = "UUID", nullable = false, length = 40)
    private String uuid;

    @ManyToOne
    @JoinColumn(name = "PARENT_CATEGORY_ID")
    private Category parentCategory;

    @OneToMany(mappedBy = "parentCategory")
    private Collection<Category> subCategories = new LinkedHashSet<Category>();
}


错误是:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.hemanths.expense.manager.api.hibernate.entity.Category.subCategories, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
    at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266)


有人可以帮我找到解决方案吗?

最佳答案

查看您的堆栈跟踪,看起来这是一个会话管理问题。尝试查看打开会话的位置以及正在使用的SessionContext?

关于java - org.hibernate.LazyInitializationException:无法延迟初始化角色集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28823366/

10-12 02:43