今天做了个hibernate的双向一对多,但老在paper.getReplys().add(reply)这里出错,我加个paper.getReply().size()也出错,错误如下:
java.lang.RuntimeException: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
com.bal.hibernate.replyService.addReply(replyService.java:49)
不知道什么原因,我把lazy设为false就出现另外的错误了,各位大虾,帮忙看下!!!!
java.lang.RuntimeException: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
com.bal.hibernate.replyService.addReply(replyService.java:49)
不知道什么原因,我把lazy设为false就出现另外的错误了,各位大虾,帮忙看下!!!!
解决方案 »
- jeasy.analysis.MMAnalyzer这个包咋装,咋导入工程啊?
- 多线程问题
- 内部类利用出现问题
- 用JForum做内核,用DIscuz做界面的论坛
- 如何把画的矩形通过按钮实现放大缩小(基本框架我基本写完)
- 两个帖子200分求助java实现文件操作(zhuyedeqingqing也是我)
- 新手请问,<identifier> expected是什么原因呢?
- 高分求关于文RandomAccessFile类的错误修改,谢谢。
- 请问java2用什么编译器和编辑器
- poi中总是显示org.apache.poi.hssf.usermodel不存在
- 谁给我分析一下这两段代码呀?谢谢哦~~
- java程序运行时是先执行main方法还是先进行初始化?大伙都来讨论讨论!
Transaction tx = null;
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
Paper paper = (Paper)session.load(Paper.class,replyid);
reply.setPaper(paper);
//paper.getReplys().size();//这里出错
paper.getReplys().add(reply);//去掉上面,这里也出错
tx.commit();
}catch(Exception e){
try {
tx.rollback();
}catch(HibernateException ex) {
System.out.println("reply add transaction error:" + ex);
}
System.out.println("reply add paper hibernate error:" + e);
throw new RuntimeException(e);
}finally {
if (session != null) {
try {
SessionFactory.closeSession();
}catch(HibernateException ex){
System.out.println("addreply close session error:" + ex);
throw new RuntimeException(ex);
}
}
}
Paper.hbm.xml:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Thu Jan 12 15:56:36 CST 2006 -->
<hibernate-mapping package="com.bal.hibernate"> <class name="Paper" table="paper">
<id name="id" column="id" type="short">
<generator class="identity"/>
</id>
<property name="title" column="title" type="string" />
<property name="content" column="content" type="string" />
<property name="author" column="author" type="string" />
<property name="createdate" column="createdate" type="timestamp" />
<property name="hotcount" column="hotcount" type="integer" />
<property name="replycount" column="replycount" type="integer" />
<property name="computername" column="computername" type="string" />
<property name="picture" column="picture" type="string" />
<set name="replys" inverse="true" lazy="true" cascade="save-update">
<key column="replyid"/>
<one-to-many class="com.bal.hibernate.Reply"/>
</set>
</class>
</hibernate-mapping>
Reply.hbm.xml:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Mon Feb 13 14:02:45 CST 2006 -->
<hibernate-mapping package="com.bal.hibernate"> <class name="Reply" table="reply">
<id name="id" column="id" type="short">
<generator class="identity"/>
</id>
<many-to-one name="paper" column="replyid" cascade="none" not-null="true"
class="com.bal.hibernate.Paper">
</many-to-one>
<property name="title" column="title" type="string" />
<property name="content" column="content" type="string" />
<property name="author" column="author" type="string" />
<property name="createdata" column="createdata" type="timestamp" />
<property name="computername" column="computername" type="string" />
<property name="picture" column="picture" type="string" />
</class>
</hibernate-mapping>
/*
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*
* Created Thu Jan 12 15:56:36 CST 2006 by MyEclipse Hibernate Tool.
*/
package com.bal.hibernate;import java.io.Serializable;
import java.util.Set;
import java.util.HashSet;/**
* A class that represents a row in the paper table.
* You can customize the behavior of this class by editing the class, {@link Paper()}.
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*/
public abstract class AbstractPaper
implements Serializable
{ /** The value of the simple id property. */
private java.lang.Short id; /** The value of the simple title property. */
private java.lang.String title; /** The value of the simple content property. */
private java.lang.String content; /** The value of the simple author property. */
private java.lang.String author; /** The value of the simple createdate property. */
private java.util.Date createdate; /** The value of the simple hotcount property. */
private java.lang.Integer hotcount; /** The value of the simple replycount property. */
private java.lang.Integer replycount; /** The value of the simple computername property. */
private java.lang.String computername; /** The value of the simple picture property. */
private java.lang.String picture; private Set replys = new HashSet();
/**
* Simple constructor of AbstractPaper instances.
*/
public AbstractPaper()
{
} /**
* Return the value of the id column.
* @return java.lang.Short
*/
public java.lang.Short getId()
{
return this.id;
} /**
* Set the value of the id column.
* @param id
*/
public void setId(java.lang.Short id)
{
this.id = id;
} /**
* Return the value of the title column.
* @return java.lang.String
*/
public java.lang.String getTitle()
{
return this.title;
} /**
* Set the value of the title column.
* @param title
*/
public void setTitle(java.lang.String title)
{
this.title = title;
} /**
* Return the value of the content column.
* @return java.lang.String
*/
public java.lang.String getContent()
{
return this.content;
} /**
* Set the value of the content column.
* @param content
*/
public void setContent(java.lang.String content)
{
this.content = content;
} /**
* Return the value of the author column.
* @return java.lang.String
*/
public java.lang.String getAuthor()
{
return this.author;
} /**
* Set the value of the author column.
* @param author
*/
public void setAuthor(java.lang.String author)
{
this.author = author;
} /**
* Return the value of the createdate column.
* @return java.util.Date
*/
public java.util.Date getCreatedate()
{
return this.createdate;
} /**
* Set the value of the createdate column.
* @param createdate
*/
public void setCreatedate(java.util.Date createdate)
{
this.createdate = createdate;
} /**
* Return the value of the hotcount column.
* @return java.lang.Integer
*/
public java.lang.Integer getHotcount()
{
return this.hotcount;
} /**
* Set the value of the hotcount column.
* @param hotcount
*/
public void setHotcount(java.lang.Integer hotcount)
{
this.hotcount = hotcount;
} /**
* Return the value of the replycount column.
* @return java.lang.Integer
*/
public java.lang.Integer getReplycount()
{
return this.replycount;
} /**
* Set the value of the replycount column.
* @param replycount
*/
public void setReplycount(java.lang.Integer replycount)
{
this.replycount = replycount;
} /**
* Return the value of the computername column.
* @return java.lang.String
*/
public java.lang.String getComputername()
{
return this.computername;
} /**
* Set the value of the computername column.
* @param computername
*/
public void setComputername(java.lang.String computername)
{
this.computername = computername;
} /**
* Return the value of the picture column.
* @return java.lang.String
*/
public java.lang.String getPicture()
{
return this.picture;
} /**
* Set the value of the picture column.
* @param picture
*/
public void setPicture(java.lang.String picture)
{
this.picture = picture;
}
public Set getReplys() {
return this.replys;
}
public void setReplys(Set replys) {
this.replys = replys;
}
}
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*
* Created Mon Feb 13 14:02:46 CST 2006 by MyEclipse Hibernate Tool.
*/
package com.bal.hibernate;import java.io.Serializable;/**
* A class that represents a row in the reply table.
* You can customize the behavior of this class by editing the class, {@link Reply()}.
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*/
public abstract class AbstractReply
implements Serializable
{ /** The value of the simple id property. */
private java.lang.Short id; /** The value of the simple replyid property. */
private java.lang.Integer replyid; /** The value of the simple title property. */
private java.lang.String title; /** The value of the simple content property. */
private java.lang.String content; /** The value of the simple author property. */
private java.lang.String author; /** The value of the simple createdata property. */
private java.util.Date createdata; /** The value of the simple computername property. */
private java.lang.String computername; /** The value of the simple picture property. */
private java.lang.String picture; private Paper paper;
/**
* Simple constructor of AbstractReply instances.
*/
public AbstractReply()
{
} public Paper getPaper() {
return this.paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
/**
* Return the value of the id column.
* @return java.lang.Integer
*/
public java.lang.Short getId()
{
return this.id;
} /**
* Set the value of the id column.
* @param id
*/
public void setId(java.lang.Short id)
{
this.id = id;
} /**
* Return the value of the replyid column.
* @return java.lang.Integer
*/
public java.lang.Integer getReplyid()
{
return this.replyid;
} /**
* Set the value of the replyid column.
* @param replyid
*/
public void setReplyid(java.lang.Integer replyid)
{
this.replyid = replyid;
} /**
* Return the value of the title column.
* @return java.lang.String
*/
public java.lang.String getTitle()
{
return this.title;
} /**
* Set the value of the title column.
* @param title
*/
public void setTitle(java.lang.String title)
{
this.title = title;
} /**
* Return the value of the content column.
* @return java.lang.String
*/
public java.lang.String getContent()
{
return this.content;
} /**
* Set the value of the content column.
* @param content
*/
public void setContent(java.lang.String content)
{
this.content = content;
} /**
* Return the value of the author column.
* @return java.lang.String
*/
public java.lang.String getAuthor()
{
return this.author;
} /**
* Set the value of the author column.
* @param author
*/
public void setAuthor(java.lang.String author)
{
this.author = author;
} /**
* Return the value of the createdata column.
* @return java.util.Date
*/
public java.util.Date getCreatedata()
{
return this.createdata;
} /**
* Set the value of the createdata column.
* @param createdata
*/
public void setCreatedata(java.util.Date createdata)
{
this.createdata = createdata;
} /**
* Return the value of the computername column.
* @return java.lang.String
*/
public java.lang.String getComputername()
{
return this.computername;
} /**
* Set the value of the computername column.
* @param computername
*/
public void setComputername(java.lang.String computername)
{
this.computername = computername;
} /**
* Return the value of the picture column.
* @return java.lang.String
*/
public java.lang.String getPicture()
{
return this.picture;
} /**
* Set the value of the picture column.
* @param picture
*/
public void setPicture(java.lang.String picture)
{
this.picture = picture;
}
}