在我的项目中,我使用PostgreSQL和Hibernate连接到我的ZUL和所有杂音。我正在尝试以Facebook方式进行的连接是一个用户条目,其ID出现在多个帖子和评论中。这需要进行一对多交易和多对一交易。这是我第一次在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,Comment和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的连接器基本上是用不同的表名和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;
}
感谢您的帮助!
答案 0 :(得分:0)
问题在于,当您尝试检索Fbuser的帖子时,该会话已关闭。显然,急于加载无法与您检索数据的方式一起使用。您有两种选择:
1)肮脏的hack:检索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;
}
可能最好,最有效的方法是删除急切的加载并根据需要检索数据,这看起来与您正在执行的操作非常接近。
答案 1 :(得分:0)
得知我的错误并没有出现在控制器中,而是在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"
和多对一/一对多标签时,问题就来了!