"failed to lazily initialize a collection of role"

"failed to lazily initialize a collection of role"

在我的项目中,我使用 PostgreSQL 和 Hibernate 连接到我的 ZUL 和所有这些噪音。我试图做的连接是,以 Facebook 的方式,一个用户条目,其 ID 出现在几个 Posts 和评论中。这需要 OneToMany 和 ManyToOne 类型的交易。这是我第一次在 ZK 和 Hibernate 中进行此类连接,如果能指出我错误的地方,我将不胜感激。

以下是多次弹出的错误:

Caused by: org.zkoss.zel.ELException: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Grave:   org.zkoss.zk.ui.UiException: org.hibernate.LazyInitializationException: could not initialize proxy - no Session at [file:/C:/Users/Frank/Documents/Work/Fakebook_v3/build/web/index.zul, line:42]
Grave:   org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: pojos.Post.comments, could not initialize proxy - no Session

下面是 Post、评论和 FBUser java 文件:

@Entity
@Table(name = "post")
@org.hibernate.annotations.Entity(dynamicUpdate = true)
public class Post implements java.io.Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "postid", updatable = false, nullable = false)
private int postid;

@Column(name = "fbuser")
private Fbuser fbuser;

@Column(name = "info")
private String info;

@Column(name = "plikes")
private int plikes;

@Column(name = "comments")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "post", cascade = CascadeType.ALL)
private Collection<Comment> comments = new LinkedHashSet<Comment>();

//getters, setters and other tools

@Entity
@Table(name = "comment")
@org.hibernate.annotations.Entity(dynamicUpdate = true)
public class Comment implements java.io.Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "commentid", updatable = false, nullable = false)
private int commentid;

@Column(name = "fbuser")
private Fbuser fbuser;

@Column(name = "post")
@ManyToOne
@JoinColumn(name="post", nullable=false)
private Post post;

@Column(name = "info")
private String info;

@Column(name = "clikes")
private int clikes;

//getters, setters and other tools

@Entity
@Table(name = "fbuser")
@org.hibernate.annotations.Entity(dynamicUpdate = true)
public class Fbuser implements java.io.Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "fbuserid", updatable = false, nullable = false)
private int fbuserid;

@Column(name = "name")
private String name;

@Column(name = "comments")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "fbuser", cascade = CascadeType.ALL)
private Set<Comment> comments = new HashSet<Comment>(0);

@Column(name = "posts")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "fbuser", cascade = CascadeType.ALL)
private Collection<Post> posts = new LinkedHashSet<Post>();

//getters, setters and other tools

我与 Hibernate 的连接器基本上是这段代码多次使用不同的 table 名称和 ID:

public List<Post> getPosts() throws Exception {
    session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();

    String toDo = "FROM Post ORDER BY postid DESC";

    List<Post> posts = session.createQuery(toDo).list();

    session.getTransaction().commit();
    session.close();
    return posts;
}

感谢任何帮助!

问题是当您想尝试检索 Fbuser 的帖子时会话已关闭。显然,急切加载不适用于您检索数据的方式。您有几个选择:

1) 肮脏的黑客:当你检索 Fbuser

时主动加载帖子
String toDo = "FROM Fbuser ORDER BY fbuserid DESC";
List<Fbuser> fbusers = session.createQuery(toDo).list();
for (Fbuser fbuser : fbusers)
    Hibernate.initialize(fbuser.getPosts());

这样做的缺点是您可能需要循环浏览帖子并获取评论。如果有大量用户、帖子和评论,则非常丑陋并且可能非常耗时。

2) 以休眠方式执行查询,以便实现预先加载:

public Fbuser getFbuserById(long fbuserid) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Fbuser fbuser = session.get(Fbuser.class, fbuserid);
    session.close();
    return fbuser;
}

可能最好、最有效的方法是删除预先加载并根据需要检索数据,这看起来与您正在做的非常接近。

得知我的错误不在控制器中,而是在文件 Post、Comment 和 Fbuser 的 hbm.xml 中,我感到很惊讶。 在这些文件中,我以各种方式有这些行:

<many-to-one name="fbuser" class="pojos.Fbuser" lazy="false" fetch="select">
    <column name="fbuser_id" not-null="true" />
</many-to-one>
<property name="info" type="string">
    <column name="info" length="300" not-null="true" />
</property>
<property name="plikes" type="int">
    <column name="plikes" not-null="true" />
</property>
<set name="comments" table="comment" inverse="true" lazy="false" fetch="select">
    <key>
        <column name="post_id" not-null="true" />
    </key>
    <one-to-many class="pojos.Comment" />
</set>

当我没有在集合中添加属性 lazy="false" 和 many-to-one/one-to-many 标签时,问题就来了!