0

"failed to lazily initialize a collection of role"

asked 2019-05-17 06:47:09 +0800

FrankV427 gravatar image FrankV427
5 2

In my project, I use PostgreSQL and Hibernate to connect to my ZUL and all that noise. The connection I am trying to do is, in a Facebook manner, an user entry whose ID appears in several Posts and Comments. This calls for a OneToMany and a ManyToOne kind of deal. This is my first time doing these kind of connections in ZK and Hibernate, and I would appreciate it if I were pointed out where I'm wrong.

Here are the errors that pop out several times:

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

And here are the Post, Comment and FBUser java files:

@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

And my connector with Hibernate is basically this code several times with different table names and ID's:

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;
}

Any help is appreciated!

delete flag offensive retag edit

Comments

You are accesing the roles when your hibernate session is already closed. Downpart of hibernate lazy collections

chillworld ( 2019-05-23 17:09:46 +0800 )edit

Eager fetch will avoid this problem or set up mechanisme that will use a new hibernate session to fetch the roles

chillworld ( 2019-05-23 17:11:36 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted
0
link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2019-05-17 06:47:09 +0800

Seen: 13 times

Last updated: Dec 01 '20

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More